8. LOOPZ / LOOPE Instructions
Loop if Zero / Loop if Equal.
They are like LOOP instruction except that they have one
additional condition:
The Zero flag must be set in order for control to transfer
to the destination label.
Loopz/Loope destination
9. They perform the following tasks:
Otherwise, no jump occurs, and control passes to the
next instruction
ECX = ECX - 1
if ECX > 0 and ZF = 1, jump to
destination
10. LOOPNZ / LOOPNE Instructions
Loop if Not Zero / Loop if Not Equal.
The Counterpart of LOOPZ and LOOPE.
The Zero flag must be Clear in order for control to
transfer to the destination label.
Loopnz/Loopne destination
11. They perform the following tasks:
Otherwise, no jump occurs, and control passes to the
next instruction
ECX = ECX - 1
if ECX > 0 and ZF = 0, jump to
destination
13. Min and Max in Array
Write an assembly program that finds the min and max
of an entered integer array.
14. arr_size equ 5
.data
arr DWORD arr_size DUP(0)
min DWORD 9fffffffh
max DWORD 0
strPrompt BYTE 'Enter array items:', 0
strMinMsg BYTE 'The min value = ', 0;a message
to be displayed to user
strMaxMsg BYTE 'The Max value = ', 0;a message
to be displayed to user
15. .code
main PROC
mov edx, offset strPrompt ;put the address of
strPrompt in edx
call WriteString;as a parameter to WriteString
MOV ESI, OFFSET arr
MOV ECX, arr_size
L1:
CALL ReadDec
MOV [ESI], EAX ;what saved frm ReadDec
ADD ESI, TYPE arr ;update esi
CMP EAX, min ;compare with min
JB updateMin ;update min if there is a new min
CMP EAX, max ;compare with max
JA updateMax ;update max if there is a new max
LOOP L1 ;this line will be hit ... when??
JMP next
16. updateMin:
MOV min, EAX ;update the min
Loop L1 ;continue loop if ECX > 0
JMP next ;GOTO the end if loop is done
updateMax:
MOV max, EAX ;update the max
Loop L1 ;continue loop if ECX > 0
JMP next ;GOTO the end if loop is done
17. next:
MOV EDX, OFFSET strMinMsg ;print the min msg
CALL WriteString
MOV EAX, min ;print the min
Call WriteDec
CALL CRLF
MOV EDX, OFFSET strMaxMsg ;print the max msg
CALL WriteString
MOV EAX, max
CALL WriteDec ;display the max
Call CRLF
exit
main ENDP
18. Student Grade
Write an assembly program that inputs a student’s score
and prints her/his grade. The student will be “Excellent”
if her/his score is between 90‐100, “Very Good” if the
score is between 80‐89, “Good” if the score is between
70‐79, “Fair” if the score is between 60‐69, and “Fail” if
the score is lower than 60. (Assume scores are integers.)
19. New Directives
This problem can be done using CMP && JMP, but here
we will introduce new directives to do the same
functionality
22. Student Grade
Write an assembly program that inputs a student’s score
and prints her/his grade. The student will be “Excellent”
if her/his score is between 90‐100, “Very Good” if the
score is between 80‐89, “Good” if the score is between
70‐79, “Fair” if the score is between 60‐69, and “Fail” if
the score is lower than 60. (Assume scores are integers.)
26. Grades Counter
Write an assembly program that accepts multiple
students’ scores, and then prints number of students in
each grade. Grades and their score ranges are defined in
the previous exercise. The program should also print an
error message if the entered score is less than 0 or
greater than 100.
27. scores_cnt equ 5
.data
prompt byte "Enter 5 scores: ",0
promptA byte "Studens Have Excellent ",0
CntA DWORD 0
promptB byte "Students Have Very Good ",0
CntB DWORD 0
promptC byte "Students Have Good ",0
CntC DWORD 0
promptD byte "Students Have Fair ",0
CntD DWORD 0
promptF byte "Students Have Fail ",0
CntF DWORD 0
promptE byte "Error!!",0
32. What If the Problem …
takes a student score, prints its grade then asks user if
s/he wants to enter another score
Repeat OR While
33. .REPEAT & .WHILE
The .REPEAT directive executes the loop body before
testing the runtime condition following the .UNTIL
directive. However, the .WHILE directive tests the
condition before executing the body loop
.REPEAT
Statements
.UNTIL cond
.WHILE cond
Statements
.ENDW
Do then
check
Check
then
DO
34. .data
prompt byte "Enter a student score: ",0
;print the output msg
promptA byte "Excellent",0
promptB byte "Very Good",0
promptC byte "Good",0
promptD byte "Fair",0
promptF byte "Fail",0
promptE byte "Error!!",0
stragain byte "Do you want to enter another
score (Y/N)? ",0
38. .code
main PROC
mov ecx, SIZEOF original
mov esi, 0 ;index for original string
mov edi, ecx ;index for reversed string
dec edi ;because array is zero indexed
dec edi ;skip the null termination char
L:
mov al, original[edi]
mov reversed[esi], al
inc esi ;slide to next character
dec edi
loop L
mov reversed[esi], 0 ;null-terminate the
reversed string
mov edx, offset reversed
call writestring
call crlf
39. Find the Non Zero …
Write an Assembly program that looks for the first
nonzero value in an array of 16-bit integers.
If it finds one, it displays the value; otherwise, it displays
a message stating that a nonzero value was not found.
0, 0, 0, 0, -1, 20, 35, -12, 66, 4, 0
Test
Case
40. intArray SWORD 0, 0, 0, 0, -1, 20, 35, -12, 66,
4, 0
noneMsg BYTE "A non-zero value was not found", 0
41. mov ebx, OFFSET intArray ;point to the array
mov ecx, LENGTHOF intArray ;loop counter
L1:
cmp WORD PTR [ebx], 0 ;compare value to zero
jnz found ;found a value
add ebx, TYPE intArray ;point to next
loop L1 ;continue the loop
jmp notFound ;none found
found: ;display the value
movsx eax, WORD PTR[ebx] ;sign-extend into EAX
call WriteInt
jmp quit
notFound: ;display "not found" message
mov edx, OFFSET noneMsg
call WriteString
quit:
call Crlf
exit
42. mov ebx, OFFSET intArray ;point to the array
mov ecx, LENGTHOF intArray ;loop counter
L1:
cmp WORD PTR [ebx], 0 ;compare value to zero
jnz found ;found a value
add ebx, TYPE intArray ;point to next
loop L1 ;continue the loop
jmp notFound ;none found
found: ;display the value
movsx eax, WORD PTR[ebx] ;sign-extend into EAX
call WriteInt
jmp quit
notFound: ;display "not found" message
mov edx, OFFSET noneMsg
call WriteString
quit:
call Crlf
exit
43. MOVSX VS.MOVZX Real Example
The output will be -1
movsx eax, WORD PTR[ebx] ;sign-extend into EAX
call WriteInt
movzx eax, WORD PTR[ebx] ;zero-extend into EAX
call WriteInt
The output will be +65535 EAX = 0000FFFF
EAX = FFFFFFFF
44. MOVSX VS.MOVZX Real Example
The output will be 65535
movsx eax, WORD PTR[ebx] ;sign-extend into EAX
call WriteDec
movzx eax, WORD PTR[ebx] ;zero-extend into EAX
call WriteDec
The output will be 4294967295
EAX = 0000FFFF
EAX = FFFFFFFF
46. Triangle of *
Write an assembly program that reads the size of a right
triangle and then prints it by asterisks. For example, if
your program reads a size of 5, it should print:
*
**
***
****
*****
Hint: WriteChar is an Irvine function that prints a single
character. This character must be stored in AL register.
47. Two Largest…
Write an assembly program that takes 10 integers and
finds the two largest values among input integers
PTR is an operator that overrides the size of an operand. It is always preceded by a Type (BYTE, WORD, DWORD…etc). In the instruction add eax, DWORD PTR [esi], you can remove DWORD PTR as the assembler will assume a default size equals to the size of the second operand which in this case DWORD. If ax is used instead of eax, WORD size will be assumed and so on.
Writeint function is a function defined in Irvine library. It prints a signed integer stored in EAX on the screen.
LENGTHOF operator retrieves the length of an array. For example, the instruction mov ECX, LENGTHOF Arr1 gets the length of Arr1 array and stores it in ECX.
TYPE operator retrieves the number of bytes allocated for each item in the given array. For example, the instruction add esi, TYPE Arr1 adds 4 to esi if Arr1 is DWORD array, adds 2 if Arr1 is WORD array and adds 1 if Arr1 is BYTE array
ReadInt function is a function defined in Irvine library. It reads a 32‐bit signed decimal integer from the user and stores it in EAX Register, stopping when the Enter key is pressed, leading spaces are ignored, and an optional leading + or ‐ sign is permitted. ReadInt will display an error message, set the Overflow flag, and reset EAX to zero if the value entered cannot be represented as a 32‐bit signed integer. If you need to store the entered value in a variable, you need to move it from EAX to this variable.
ReadDec is the same for Unsigned
.IF eax > -1 statement is translated into JBE instruction assuming unsigned
numbers. However, .IF eax > int1 is translated into JLE instruction
assuming signed numbers because int1 is defined as SDWORD.
The idea is in parsing the value stored in EAX based on what you call either WriteInt or WriteDec
In case of WriteInt – It parse the value that it’s a signed value or in other words the output must have + or – sign leading
1.It know the SIGN from the last (MSB) bit
2.it reads the remaining value and prints it as it understood
In case of WriteDec – It assume that the value is an UNSIGNED value
1.No signs appears here
2.It parsed the whole value as it is