Solved

Assembly........How to read 50 integers from a file

Posted on 2000-04-07
7
185 Views
Last Modified: 2010-04-02
Hi. I am trying to do a program that reads 50 integers from a file and calculates their average. I have no idea how to read from a file. Please if anyone knows I will appreciate you explain. Thanx
It is for an 8086
0
Comment
Question by:stinger_maniac
  • 4
  • 3
7 Comments
 
LVL 22

Accepted Solution

by:
nietod earned 60 total points
Comment Utility
x86 assembly (well, no assembly language) doesn't provide any means to read from a file.   That has to be done through the use of an OS-specific technique.  i.e. by calling an OS-procedure or by performing a software interrupt to an OS service.

So we woud have to know what OS you are running on in order to read the file.

Also since the language varies from assembler to assembler  it would be good to know what assembler you are using

To do the average, though

INTARY DW 50 DUP (0)

   *   *   *


   MOV CX,50 ; Get number of ints to add.
   MOV BX,OFFSET INTARY ; Get -> 1st int to add.
   XOR AX,AX ; Clear the low word of the total.
   XOR DX,DX ; Clear the high word of the total.

CALAVGTOP: ADD AX,WORD PTR[BX]; Add int to low word of total.
    ADC,DX,0 ; Add carry to high word.
    ADD,BX,2 ; Point to next int.
    LOOP CALAVGTOP ; Loop back for next int.

  MOV BX,50 ; Get number of ints to divide by.
  DIV BX ; Calculate average in AX.

Let me know if you have any questions or any other information.
0
 

Author Comment

by:stinger_maniac
Comment Utility
It is for dos and it is MASM 5.0
0
 
LVL 22

Expert Comment

by:nietod
Comment Utility
INTARY DW 50 DUP (0)
FILNAM DB "C:\SOMEFILE.DAT",0

   *  *  *
; Assuming DS is set to the segment with the file naem and array
  MOV DX,OFFSET FILNAM ; Get -> file name
  MOV AL,0 ; Specify open read only, deny none.
  MOV AH,3DH ; Specify open file function
  INT 21H ; Open the file.
  JC ERRORHANDLER
  MOV BX,AX ; Get the file handle.
  MOV DX,OFFSET INTARY ; Get -> array.
  MOV CX,2*50 ; Get number of bytes to read.
  MOV AH,3FH ; Specify file read function.
  INT 21H ; Read the file.
  JC ERRORHANDLER
  MOV AH 3EH ; Specify close file function.
  INT 21H ; Close the file.
0
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 

Author Comment

by:stinger_maniac
Comment Utility
can you explain a little bit what you are doing there?like comment the code
what does *  *  *  stand for?
thanx
=)
0
 

Author Comment

by:stinger_maniac
Comment Utility
Adjusted points from 50 to 60
0
 
LVL 22

Expert Comment

by:nietod
Comment Utility
I commented everything.  It just opens a file, reads out 100 bytes and closes the file.  All done through DOS service interrupts.

The *  * * is to break up the code intop sections as you will probably declare your data in a seperate part of the program from the code.
0
 

Author Comment

by:stinger_maniac
Comment Utility
thanx
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now