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

Posted on 2000-04-07
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
Question by:stinger_maniac
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
LVL 22

Accepted Solution

nietod earned 60 total points
ID: 2692952
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


   *   *   *

   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.

Author Comment

ID: 2694309
It is for dos and it is MASM 5.0
LVL 22

Expert Comment

ID: 2694446

   *  *  *
; 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.
  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.
  MOV AH 3EH ; Specify close file function.
  INT 21H ; Close the file.
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Author Comment

ID: 2694812
can you explain a little bit what you are doing there?like comment the code
what does *  *  *  stand for?

Author Comment

ID: 2694941
Adjusted points from 50 to 60
LVL 22

Expert Comment

ID: 2694954
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.

Author Comment

ID: 2703226

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
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 concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

695 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