An Assembly Language

 

 

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:

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.

Sample Solution

Here’s an example assembly language program (x86) that reads movie review information and calculates the total score for each movie:

Code snippet
; Define number of reviewers and movies
NUM_REVIEWERS EQU 4
NUM_MOVIES EQU 5

; Data segment
.data
fileName db "reviews.txt", 0 ; File name
movieLetters db "A", "B", "C", "D", "E", 0 ; Movie identifiers

; Declare two-dimensional array for review scores
reviews resb NUM_REVIEWERS * NUM_MOVIES * 4 ; Reserve space for scores (4 bytes each)

newline db 10, 13, 0 ; Newline character sequence

; Code segment
.code
main PROC

    ; Open the file for reading
    mov eax, 3 ; Open system call
    mov ebx, 5 ; Open for reading
    mov ecx, offset fileName ; File name address
    mov edx, 0 ; No additional flags
    int 0x80

    ; Check if file opened successfully
    cmp eax, -1
    je error_open

    ; Read each line of the file
loop_read:
    mov eax, 3 ; Read system call
    mov ebx, STDIN_FILENO ; Standard input (file handle)
    mov ecx, offset buffer ; Buffer address
    mov edx, 100 ; Buffer size (max characters per line)
    int 0x80

    ; Check for end of file (EOF)
    cmp eax, 0
    je loop_end

    ; Process the read line
    call parse_line ; Parse the line for movie, score, and reviewer

    jmp loop_read ; Read next line

loop_end:
    ; Close the file
    mov eax, 4 ; Close system call
    mov ebx, STDIN_FILENO ; File handle
    int 0x80

    ; Calculate total score for each movie
    call calculate_totals

    ; Print movie review report
    call print_report

exit:
    mov eax, 1 ; Exit system call
    mov ebx, 0 ; Exit code
    int 0x80

error_open:
    ; Print error message if file failed to open
    mov eax, 4 ; Write system call
    mov ebx, 1 ; Standard output (file handle)
    mov ecx, offset error_msg ; Error message address
    mov edx, lengthof error_msg ; Message length
    int 0x80

    jmp exit ; Exit program

; Function to parse a line from the file
parse_line PROC
    ; Extract movie letter
    mov esi, offset buffer ; Buffer address
    mov bl, [esi] ; Get first character
    cmp bl, 'A'
    jl invalid_line ; Less than 'A', invalid line
    cmp bl, 'E'
    jg invalid_line ; More than 'E', invalid line
    sub bl, 'A' ; Convert to index (0-4)
    mov dx, bl ; Store movie index

    ; Skip comma
    inc esi ; Move to next character
    cmp byte ptr [esi], ','
    jne invalid_line ; Not a comma, invalid line

    ; Extract score (convert ASCII to integer)
    inc esi ; Move to next character
    mov al, 0 ; Clear accumulator
loop_digit:
    cmp byte ptr [esi], 0
    je end_digit ; End of string
    cmp byte ptr [esi], '0'
    jl invalid_line ; Not a digit, invalid line
    cmp byte ptr [esi], '9'
    jg invalid_line ; Not a digit, invalid line
    sub byte ptr [esi], '0' ; Convert ASCII digit to value
    mul 10 ; Multiply by 10 for next digit
    add al, byte ptr [esi] ; Add current digit
    inc esi ; Move to next character
    jmp loop_digit
end_digit:
    mov word ptr reviews[bl * NUM_MOVIES * 4], ax ; Store score in array

    ; Skip comma
    inc esi ; Move to next character
    cmp byte ptr [esi], ','
    jne invalid_line ; Not a comma, invalid line

    ; Extract reviewer number (convert ASCII to integer)
    inc esi ; Move to next character
    mov al, 0 ; Clear accumulator
loop_reviewer:
    cmp byte ptr [esi], 0
    je end_reviewer ; End of string
    cmp byte ptr [esi], '1'
    jl invalid

This question has been answered.

Get Answer