# Using MARS, write Assembly code (for RISC) that computes average of list of mi
ID: 3533057 • Letter: #
Question
# Using MARS, write Assembly code (for RISC) that computes average of list of mideterm test scores in # freshman ENGR121 class and return in $v0.
# The list consists of scores from 0 through 100 and is terminated with -1.
# Ignore all other values < 0 and > 100.
#
# sum = count = 0;
# while ( *score != -1) do {
# sum += *score;
# count += 1;
# score += 4;
# }
# average = sum / count;
#
# Inputs :
# $a0 - address of list of scores
# Outputs:
# $v0 - returns the average of the test scores or -1.
# Locals:
# $t0 = score counter
# $t1 = score sum
# $t2 = temp for score
# $t3 = temp
# $t4 = holds terminator marker, -1
#
# scores: .word 95, 92, 85, 100, 81, 151, 90, 75, -85, 99, 82, 79, -1
Explanation / Answer
title Average
.model small
.stack 100h
CR = 13
LF = 10
EOS = '$'
EF = 101 ; 101 ends data input
.data
prompt db "Enter numbers to average [101 ends input]: ", EOS
newline db CR, LF, EOS
count dw ?
avg = 0
.code
main proc
mov ax, @data ; setup data segment reg
mov ds, ax
mov dx, offset prompt
mov ah, 9h ; display string function
int 21h ; call os
;
; read a number from the stdin, no error checking
mov dx, 0 ; running sum = sum + new digit
mov cx, 10 ; base 10
while1: mov ah, 01h ; get a digit
int 21h
cmp al, cr ; if cr, then done
je endw1
;;mov al, 7 ; faking input as a 7
;;sub al, '0' ; turn ascii digit to binary
mov ah, 0 ; wordsize digit
mov bx, ax ; save new digit
mov ax, dx ; sum * 10
mul cx ;
mov dx, bx ; get back new digit
add dx, ax ; now + new digit
jmp while1 ; next digit...
endw1:
;;number is XXXXX dx
;; add sum and increment count
cmp dx, 101 ;
je endw2
mov ax, sum ; get sum
add ax, dx ;
mov sum, ax ;
inc count
jmp while1 ; get number again
endw2:
;; perform average calculation
;; to do
mov count, dx
mov dx, offset newline
mov ah, 09h
int 21h
main endp
end main ; execution entry point
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.