Assembly Language for x86 Processors- read file and store into a 2d array and print highest score and totals

Hello, I need help with how to take the data from the file and store into 2d array in 86x Assembly Language, please do not do in Mars MIPS. code should use the same language as shown in the pdfs in WIndows not linux. Please utilize the the code for load a reviewer’s score into the array of movie reviews.

Write an assembly language program that reads move review information from a text file and reports the overall scores for each movie as well as identifying the movie with the highest total score. There are four movie reviewers numbered from 1 to 4. They are submitting reviews for five movies, identified by the letters from “A” through “E”. Reviews are reported by using the letter identifying the movie, the review rating, which is a number from 0 to 100, and the reviewer’s identifying number. For example, to report that movie B was rated a score of 87 by reviewer 3, there will be a line in the text file that looks like this:

B,87,3

The fields within each record are separated from each other by a comma.

Your program must store the movie review scores in a two-dimensional array (4 rows by 5 columns). Each row represents a reviewer. Each column represents a movie. Initialize the array to zeroes and read the movie review information from a file. After reading and processing the whole file, display a report that shows the total score for each movie and the movie that had the highest total score.

D,84,2
A,90,3
A,87,4
B,35,4
B,100,1
C,75,1
D,84,1
B,87,2
A,0,2
C,25,2
D,45,3
E,35,3
A,90,1
B,100,3
C,75,3
E,35,1
C,78,4
E,35,2
D,100,4
E,0,4
9.4
Two-Dimensional Arrays
369
The x86 instruction set includes two operand types, base-index and base-index-displacement,
both suited to array applications. We will examine both and show examples of how they can be
used effectively.
Figure 9–1 Row-major and column-major ordering.
Logical arrangement:
10
20
30
40
50
60
70
80
90
A0
B0
C0 D0 E0
F0
Row-major order
10
20
30
40
50
60
70
80
90
A0 B0
C0 D0 E0
F0
Column-major order
10
60
B0
20
70
C0
30
80
D0 40
90
E0 50
A0 F0
9.4.2 Base-Index Operands
A base-index operand adds the values of two registers (called base and index), producing an offset address:
[base + index]
The square brackets are required. In 32-bit mode, any 32-bit general-purpose registers may
be used as base and index registers. (Usually, we avoid using EBP except when addressing the
stack.) The following are examples of various combinations of base and index operands in 32-bit
mode:
.data
array
.code
mov
mov
mov
ebx,OFFSET array
esi,2
ax,[ebx+esi]
; AX = 2000h
mov
mov
mov
edi,OFFSET array
ecx,4
ax,[edi+ecx]
; AX = 3000h
mov
mov
mov
ebp,OFFSET array
esi,0
ax,[ebp+esi]
; AX = 1000h
WORD 1000h,2000h,3000h
370
Chapter 9 • Strings and Arrays
Two-Dimensional Array When accessing a two-dimensional array in row-major order, the
row offset is held in the base register and the column offset is in the index register. The following
table, for example, has three rows and five columns:
tableB BYTE
10h, 20h, 30h, 40h, 50h
Rowsize = ($ – tableB)
BYTE
60h, 70h, 80h, 90h, 0A0h
BYTE 0B0h, 0C0h, 0D0h, 0E0h, 0F0h
The table is in row-major order and the constant Rowsize is calculated by the assembler as
the number of bytes in each table row. Suppose we want to locate a particular entry in the table
using row and column coordinates. Assuming that the coordinates are zero based, the entry at
row 1, column 2 contains 80h. We set EBX to the table’s offset, add (Rowsize * row_index) to
calculate the row offset, and set ESI to the column index:
row_index = 1
column_index = 2
mov
add
mov
mov
ebx,OFFSET tableB
ebx,RowSize * row_index
esi,column_index
al,[ebx + esi]
; table offset
; row offset
; AL = 80h
Suppose the array is located at offset 0150h. Then the effective address represented by EBX 
ESI is 0157h. Figure 9-2 shows how adding EBX and ESI produces the offset of the byte at tableB [1,
2]. If the effective address points outside the program’s data region, a runtime error occurs.
Figure 9–2 Addressing an array with a base-index operand.
0150
10
0155
20
30
40
50
60
[ebx]
0157
70
80
90
A0 B0
C0 D0 E0
F0
[ebxesi]
Calculating a Row Sum
Base index addressing simplifies many tasks associated with two-dimensional arrays. We might,
for example, want to sum the elements in a row belonging to an integer matrix. The following
32-bit calc_row_sum procedure (see RowSum.asm) calculates the sum of a selected row in a
matrix of 8-bit integers:
;———————————————————-; calc_row_sum
; Calculates the sum of a row in a byte matrix.
; Receives: EBX = table offset, EAX = row index,
; ECX = row size, in bytes.
; Returns: EAX holds the sum.
;———————————————————-calc_row_sum PROC USES ebx ecx edx esi
mul
add
mov
mov
ecx
ebx,eax
eax,0
esi,0
;
;
;
;
row index * row size
row offset
accumulator
column index
9.4
Two-Dimensional Arrays
L1: movzx
add
inc
loop
edx,BYTE PTR[ebx + esi]
eax,edx
esi
L1
371
; get a byte
; add to accumulator
; next byte in row
ret
calc_row_sum ENDP
BYTE PTR was needed to clarify the operand size in the MOVZX instruction.
Scale Factors
If you’re writing code for an array of WORD, multiply the index operand by a scale factor of 2.
The following example locates the value at row 1, column 2:
tableW WORD
10h, 20h, 30h, 40h,
RowsizeW = ($ – tableW)
WORD
60h, 70h, 80h, 90h,
WORD 0B0h, 0C0h, 0D0h, 0E0h,
.code
row_index = 1
column_index = 2
mov
ebx,OFFSET tableW
add
ebx,RowSizeW * row_index
mov
esi,column_index
mov
ax,[ebx + esi*TYPE tableW]
50h
0A0h
0F0h
; table offset
; row offset
; AX = 0080h
The scale factor used in this example (TYPE tableW) is equal to 2. Similarly, you must use a
scale factor of 4 if the array contains doublewords:
tableD DWORD 10h, 20h, …etc.
.code
mov
eax,[ebx + esi*TYPE tableD]
9.4.3 Base-Index-Displacement Operands
A base-index-displacement operand combines a displacement, a base register, an index register,
and an optional scale factor to produce an effective address. Here are the formats:
[base + index + displacement]
displacement[base + index]
Displacement can be the name of a variable or a constant expression. In 32-bit mode, any
general-purpose 32-bit registers may be used for the base and index. Base-index-displacement
operands are well suited to processing two-dimensional arrays. The displacement can be an
array name, the base operand can hold the row offset, and the index operand can hold the column offset.
Doubleword Array Example The following two-dimensional array holds three rows of five
doublewords:
tableD DWORD
10h, 20h, 30h, 40h,
Rowsize = ($ – tableD)
DWORD
60h, 70h, 80h, 90h,
DWORD 0B0h, 0C0h, 0D0h, 0E0h,
50h
0A0h
0F0h
470
Chapter 11 • MS-Windows Programming
;
; Closes a file using its handle as an identifier.
; Receives: EAX = file handle
; Returns: EAX = nonzero if the file is successfully
;
closed.
;——————————————————-INVOKE CloseHandle, eax
ret
CloseFile ENDP
11.1.8
Testing the File I/O Procedures
CreateFile Program Example
The following program creates a file in output mode, asks the user to enter some text, writes the
text to the output file, reports the number of bytes written, and closes the file. It checks for errors
after attempting to create the file:
; Creating a File
(CreateFile.asm)
INCLUDE Irvine32.inc
BUFFER_SIZE = 501
.data
buffer BYTE BUFFER_SIZE DUP(?)
filename
BYTE “output.txt”,0
fileHandle
HANDLE ?
stringLength DWORD ?
bytesWritten DWORD ?
str1 BYTE “Cannot create file”,0dh,0ah,0
str2 BYTE “Bytes written to file [output.txt]:”,0
str3 BYTE “Enter up to 500 characters and press”
BYTE “[Enter]: “,0dh,0ah,0
.code
main PROC
; Create a new text file.
mov
edx,OFFSET filename
call CreateOutputFile
mov
fileHandle,eax
; Check for errors.
cmp
eax, INVALID_HANDLE_VALUE
jne
file_ok
mov
edx,OFFSET str1
call WriteString
jmp
quit
file_ok:
; Ask the
mov
call
mov
mov
user to input a string.
edx,OFFSET str3
WriteString
ecx,BUFFER_SIZE
edx,OFFSET buffer
; error found?
; no: skip
; display error
; “Enter up to ….”
; Input a string
11.1
Win32 Console Programming
call
mov
ReadString
stringLength,eax
471
; counts chars entered
; Write the buffer to the output file.
mov
eax,fileHandle
mov
edx,OFFSET buffer
mov
ecx,stringLength
call WriteToFile
mov
bytesWritten,eax
; save return value
call CloseFile
; Display
mov
call
mov
call
call
the return value.
edx,OFFSET str2
WriteString
eax,bytesWritten
WriteDec
Crlf
; “Bytes written”
quit:
exit
main ENDP
END main
ReadFile Program Example
The following program opens a file for input, reads its contents into a buffer, and displays the
buffer. All procedures are called from the Irvine32 library:
; Reading a File
(ReadFile.asm)
; Opens, reads, and displays a text file using
; procedures from Irvine32.lib.
INCLUDE Irvine32.inc
INCLUDE macros.inc
BUFFER_SIZE = 5000
.data
buffer BYTE BUFFER_SIZE DUP(?)
filename
BYTE 80 DUP(0)
fileHandle HANDLE ?
.code
main PROC
; Let user input a filename.
mWrite “Enter an input filename: ”
mov
edx,OFFSET filename
mov
ecx,SIZEOF filename
call ReadString
; Open the file for input.
mov
edx,OFFSET filename
call OpenInputFile
mov
fileHandle,eax
; Check for errors.
472
Chapter 11 • MS-Windows Programming
cmp
eax,INVALID_HANDLE_VALUE
; error opening file?
jne
file_ok
; no: skip
mWrite
jmp
quit
; and quit
file_ok:
; Read the file into a buffer.
mov
edx,OFFSET buffer
mov
ecx,BUFFER_SIZE
call ReadFromFile
jnc
check_buffer_size
mWrite “Error reading file. ”
call WriteWindowsMsg
jmp
close_file
; error reading?
; yes: show error message
check_buffer_size:
cmp
eax,BUFFER_SIZE
; buffer large enough?
jb
buf_size_ok
; yes
mWrite
jmp
quit
; and quit
buf_size_ok:
mov
buffer[eax],0
mWrite “File size: ”
call WriteDec
call Crlf
; insert null terminator
; display file size
; Display the buffer.
mWrite
mov
edx,OFFSET buffer
; display the buffer
call WriteString
call Crlf
close_file:
mov
eax,fileHandle
call CloseFile
quit:
exit
main ENDP
END main
The program reports an error if the file cannot be opened:
Enter an input filename: crazy.txt
Cannot open file
It reports an error if it cannot read from the file. Suppose, for example, a bug in the program used
the wrong file handle when reading the file:
Enter an input filename: infile.txt
Error reading file. Error 6: The handle is invalid.
11.1
Win32 Console Programming
473
The buffer might be too small to hold the file:
Enter an input filename: infile.txt
Error: Buffer too small for the file
11.1.9 Console Window Manipulation
The Win32 API provides considerable control over the console window and its buffer. Figure 11-1
shows that the screen buffer can be larger than the number of lines currently displayed in the console window. The console window acts as a “viewport,” showing part of the buffer.
Figure 11–1 Screen buffer and console window.
Active screen
buffer
Console window
text text text text text text text text text
text text text text text text text text text
text text text text text text text text text
text text text text text text text text text
text text text text text text text text text
text text text text texttext text text text
text text text text texttext text text text
text text text text texttext text text text
text text text text texttext text text text
text text text text texttext text text text
text text text text texttext text text text
text text text text text text text text text
text text text text text text text text text
text text text text text text text text text
text text text text text text text text text
text text text text text text text text text
text text text text text text text text text
text text text text text text text text text
text text text text text text text text text
Several functions affect the console window and its position relative to the screen buffer:
• SetConsoleWindowInfo sets the size and position of the console window relative to the
screen buffer.
• GetConsoleScreenBufferInfo returns (among other things) the rectangle coordinates of the
console window relative to the screen buffer.
• SetConsoleCursorPosition sets the cursor position to any location within the screen buffer;
if that area is not visible, the console window is shifted to make the cursor visible.
• ScrollConsoleScreenBuffer moves some or all of the text within the screen buffer, which
can affect the displayed text in the console window.
SetConsoleTitle
The SetConsoleTitle function lets you change the console window’s title. Here’s a sample:
.data
titleStr BYTE “Console title”,0
.code
INVOKE SetConsoleTitle, ADDR titleStr
Project 3 Tip – Loading Array
This code can be used to load a reviewer’s score into the array of movie
reviews:
; Insert
mov
mov
mul
size
mov
row size
mov
shl
add
offset
mov
score
mov
scores
mov
movie
score at reviews[rowIndex][colIndex]
edx,rowSize
; row size in bytes
eax,rowIndex
; row index
edx
; row index * row
index,eax
; save row index *
eax,colIndex
eax,2
eax,index
; load col index
; eax = colIndex * 4
; eax contains
edx,score
; edx = reviewer’s
ebx,OFFSET reviews
; array of review
[ebx + eax],edx
; Store score for
Section 9.4 of your textbook deals with two-dimensional arrays. There is
even an example showing how to calculate a row sum. You may be able to
adapt this example to calculate a column sum.
COSC 2425 – Programming Project 3
Write an assembly language program that reads move review information
from a text file and reports the overall scores for each movie as well as
identifying the movie with the highest total score. There are four movie
reviewers numbered from 1 to 4. They are submitting reviews for five
movies, identified by the letters from “A” through “E”. Reviews are reported
by using the letter identifying the movie, the review rating, which is a
number from 0 to 100, and the reviewer’s identifying number. For example,
to report that movie B was rated a score of 87 by reviewer 3, there will be a
line in the text file that looks like this:
B,87,3
The fields within each record are separated from each other by a comma.
Your program must store the movie review scores in a two-dimensional
array (4 rows by 5 columns). Each row represents a reviewer. Each column
represents a movie. Initialize the array to zeroes and read the movie review
information from a file. After reading and processing the whole file, display a
report that shows the total score for each movie and the movie that had the
highest total score.
Section 9.4 of our textbook discusses two-dimensional arrays. Section 9.4.2
discusses Base-Index Operands and even contains an example of how to
calculate a row sum for a two-dimensional array.
Chapter 11 contains an example program named ReadFile.asm that will
show you how to prompt the user for a file name, open a file, read its
contents, and close the file when you are done. Look in section 11.1.8,
Testing the File I/O Procedures.
Each record in a text file is terminated by the two characters, Carriage
Return (0Dh) and Line Feed (0Ah).
Assume that you wish to process a text file named “reviews.txt” that is
stored on the “C:” drive in the “Data” folder. If you are using a Windows
computer, you have two ways to identify the path to the file’s location:
C:/Data/reviews.txt OR
C:\\Data\\reviews.txt
Double backslash characters (\) are needed because a single backslash is
defined as being the first part of an escape sequence such as newline (\n).

Calculate your order
275 words
Total price: $0.00

Top-quality papers guaranteed

54

100% original papers

We sell only unique pieces of writing completed according to your demands.

54

Confidential service

We use security encryption to keep your personal data protected.

54

Money-back guarantee

We can give your money back if something goes wrong with your order.

Enjoy the free features we offer to everyone

  1. Title page

    Get a free title page formatted according to the specifics of your particular style.

  2. Custom formatting

    Request us to use APA, MLA, Harvard, Chicago, or any other style for your essay.

  3. Bibliography page

    Don’t pay extra for a list of references that perfectly fits your academic needs.

  4. 24/7 support assistance

    Ask us a question anytime you need to—we don’t charge extra for supporting you!

Calculate how much your essay costs

Type of paper
Academic level
Deadline
550 words

How to place an order

  • Choose the number of pages, your academic level, and deadline
  • Push the orange button
  • Give instructions for your paper
  • Pay with PayPal or a credit card
  • Track the progress of your order
  • Approve and enjoy your custom paper

Ask experts to write you a cheap essay of excellent quality

Place an order