Chapter 6
Conditional Processing
Assembly Language Programming Exercise
Problem # 3:
Create a procedure named CalcGrade that receives an integer value between 0 and 100, and returns a single capital letter in the AL register. Preserve all other register values between calls to the procedure. The letter returned by the procedure should be according to the following ranges:
Score Range Letter Grade
----------------------------------
90 to 100 A
80 to 89 B
70 to 79 C
60 to 69 D
0 to 59 F
Write a test program that generates 10 random integers between 50 and 100, inclusive. Each time an integer is generated, pass it to the CalcGrade procedure. You can test your program using a debugger, or if you prefer to use the book’s library, you can display each integer and its corresponding letter grade. (The Irvine32 library is required for this solution program because it uses the RandomRange procedure.)
Solution:
INCLUDE Irvine32.inc
.data
s DWORD 50
e DWORD 100
str1 BYTE "The integer score is: ",0
str2 BYTE "The letter grade is: ",0
.code
main PROC
call Clrscr
mov ecx, 10
l1:
mov eax, s
mov ebx, e
dec ebx
sub ebx, eax ;create range from 0 to N
xchg ebx, eax ;random works with eax
call RandomRange ; generate random int witin range 0 to N
neg ebx ;change sign of ebx
sub eax, ebx ;sub from eax to define range
mov edx, offset str1
call WriteString
call WriteInt
call crlf
call CalcGrade
call crlf
loop l1
call WaitMsg
exit
main ENDP
CalcGrade PROC
.IF eax >= 90 ; multiway selection structure to
mov al,'A' ; choose the correct grade letter
.ELSEIF eax >= 80
mov al,'B'
.ELSEIF eax >= 70
mov al,'C'
.ELSEIF eax >= 60
mov al,'D'
.ELSE
mov al,'F'
.ENDIF
mov edx,OFFSET str2
call WriteString
call WriteChar ; display grade letter in AL
call Crlf
ret
CalcGrade ENDP
END main
.data
s DWORD 50
e DWORD 100
str1 BYTE "The integer score is: ",0
str2 BYTE "The letter grade is: ",0
.code
main PROC
call Clrscr
mov ecx, 10
l1:
mov eax, s
mov ebx, e
dec ebx
sub ebx, eax ;create range from 0 to N
xchg ebx, eax ;random works with eax
call RandomRange ; generate random int witin range 0 to N
neg ebx ;change sign of ebx
sub eax, ebx ;sub from eax to define range
mov edx, offset str1
call WriteString
call WriteInt
call crlf
call CalcGrade
call crlf
loop l1
call WaitMsg
exit
main ENDP
CalcGrade PROC
.IF eax >= 90 ; multiway selection structure to
mov al,'A' ; choose the correct grade letter
.ELSEIF eax >= 80
mov al,'B'
.ELSEIF eax >= 70
mov al,'C'
.ELSEIF eax >= 60
mov al,'D'
.ELSE
mov al,'F'
.ENDIF
mov edx,OFFSET str2
call WriteString
call WriteChar ; display grade letter in AL
call Crlf
ret
CalcGrade ENDP
END main
Let me know in the comment section if you have any question.
Previous Post:
2. Program for Summing Array Elements in a Range in Assembly Language using Visual Studio
Comments
Post a Comment