Assembly Language (Lab 5)
AgendaRevisionConditional LoopsIF, While and Repeat DirectivesHands On
Data Transfer
Inst.
MOV
MOVZX/MOVSX
ADD/SUB
INC/DEC
NEG
Tools
ReadDec/ReadInt
Writestring
writeInt/WriteDec
OFFSET
LENGTHOF
TYPE
PTR
Addressing Modes
Types
Register Addressin
g
Immediate Addressin
g
Direct Memory
Addressing
In-Direct Memory
Addressing
Direct Offset
Addressing
Flags
Carry
Zero
Overflow
Sign
Parity
Auxiliary
Conditional LoopLOOPE/LOOPZ and there counterpart …
LOOPZ / LOOPE InstructionsLoop 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
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
LOOPNZ / LOOPNE InstructionsLoop 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
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
Hands OnLet’s make the best use of our time…
Min and Max in ArrayWrite an assembly program that finds the min and max of
an entered integer array.
arr_size equ 5.data
arr DWORD arr_size DUP(0)min DWORD 9fffffffhmax DWORD 0
strPrompt BYTE 'Enter array items:', 0strMinMsg BYTE 'The min value = ', 0;a message to be displayed to userstrMaxMsg BYTE 'The Max value = ', 0;a message to be displayed to user
.codemain PROC
mov edx, offset strPrompt ;put the address of strPrompt in edxcall WriteString;as a parameter to WriteString MOV ESI, OFFSET arrMOV ECX, arr_sizeL1:
CALL ReadDecMOV [ESI], EAX ;what saved frm ReadDecADD ESI, TYPE arr ;update esi
CMP EAX, min ;compare with minJB updateMin ;update min if there is a new minCMP EAX, max ;compare with maxJA updateMax ;update max if there is a new max
LOOP L1 ;this line will be hit ... when??JMP next
updateMin:MOV min, EAX ;update the minLoop L1 ;continue loop if ECX > 0JMP next ;GOTO the end if loop is done
updateMax:MOV max, EAX ;update the maxLoop L1 ;continue loop if ECX > 0JMP next ;GOTO the end if loop is done
next:MOV EDX, OFFSET strMinMsg ;print the min msgCALL WriteString
MOV EAX, min ;print the minCall WriteDecCALL CRLF
MOV EDX, OFFSET strMaxMsg ;print the max msgCALL WriteString
MOV EAX, maxCALL WriteDec ;display the maxCall CRLF
exitmain ENDP
Student GradeWrite 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.)
New Directives This problem can be done using CMP && JMP, but here
we will introduce new directives to do the same functionality
.IF
.IF cond1Statements[.ELSEIF cond2Statements][.ELSEStatements].ENDIF
Boolean expressions involving two
operands and a conditional
operator within them.
Possible operators are ==, !=, >, >=,
<,<=, && and ||
.IF.datauint1 dword 5uint2 dword 10int1 sdword -1
mov eax, 0.IF eax > -1
mov eax, uint2.endif
mov eax, 0.IF eax > int1
mov eax, uint2.endif
Generated Code:CMP eax, -1JBE @C0001mov eax, uint2@C0001:
Generated Code:CMP eax, int1JLE @C0002mov eax, uint2@C0002:
VS
Student GradeWrite 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.)
.dataprompt byte "Enter a student score: ",0
;print the output msg promptA byte "Excellent",0
promptB byte "Very Good",0promptC byte "Good",0promptD byte "Fair",0promptF byte "Fail",0promptE byte "Error!!",0
.codemain PROCmov edx, offset promptcall writestringcall readdec.IF eax > 100 || eax < 0
mov edx, offset promptE.ELSEIF eax >= 90
mov edx, offset promptA.ELSEIF eax >= 80
mov edx, offset promptB.ELSEIF eax >= 70
mov edx, offset promptC.ELSEIF eax >= 60
mov edx, offset promptD.ELSE
mov edx, offset promptF.ENDIF
call writestring ;print the output msg based on the prev selection
call crlfexitmain ENDP
Grades CounterWrite 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.
scores_cnt equ 5.data
prompt byte "Enter 5 scores: ",0
promptA byte "Studens Have Excellent ",0CntA DWORD 0promptB byte "Students Have Very Good ",0CntB DWORD 0promptC byte "Students Have Good ",0CntC DWORD 0promptD byte "Students Have Fair ",0CntD DWORD 0promptF byte "Students Have Fail ",0CntF DWORD 0promptE byte "Error!!",0
main PROCmov edx, offset promptcall writestringMOV ecx, scores_cnt
L1:call readdec.IF eax > 100 || eax < 0
JMP errorState.ELSEIF eax >= 90
INC CntA.ELSEIF eax >= 80
INC CntB.ELSEIF eax >= 70
INC CntC.ELSEIF eax >= 60
INC CntD.ELSE
INC CntF.ENDIF
LOOP L1JMP next
errorState:MOV edx, OFFSET promptEcall writestring call crlfJMP endState
next:MOV edx, OFFSET promptAcall writestring MOV eax, CntAcall writeDeccall crlf
MOV edx, OFFSET promptBcall writestringMOV eax, CntBcall writeDeccall crlf
MOV edx, OFFSET promptCcall writestring MOV eax, CntCcall writeDeccall crlf
MOV edx, OFFSET promptDcall writestringMOV eax, CntDcall writeDeccall crlf
MOV edx, OFFSET promptFcall writestring MOV eax, CntFcall writeDeccall crlf
endState:exit
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
.REPEAT & .WHILEThe .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
.dataprompt byte "Enter a student score: ",0
;print the output msg promptA byte "Excellent",0
promptB byte "Very Good",0promptC byte "Good",0promptD byte "Fair",0promptF byte "Fail",0promptE byte "Error!!",0
stragain byte "Do you want to enter another score (Y/N)? ",0
.codemain PROC.REPEAT
mov edx, offset promptcall writestringcall readdec.IF eax > 100 || eax < 0
mov edx, offset promptE.ELSEIF eax >= 90
mov edx, offset promptA.ELSEIF eax >= 80
mov edx, offset promptB.ELSEIF eax >= 70
mov edx, offset promptC.ELSEIF eax >= 60
mov edx, offset promptD.ELSE
mov edx, offset promptF.ENDIF
mov edx, offset stragaincall writestringcall readchar ;input char stored in AL register
.UNTIL al == 'N' || al == 'n‘
Reverse a StringWrite an Assembly program that reverses a string
.dataoriginal byte "I love Assembly :D", 0reversed byte SIZEOF original dup(?)
.codemain PROC
mov ecx, SIZEOF originalmov esi, 0 ;index for original stringmov edi, ecx ;index for reversed stringdec edi ;because array is zero indexeddec edi ;skip the null termination charL:
mov al, original[edi]mov reversed[esi], alinc esi ;slide to next character dec edi
loop Lmov reversed[esi], 0 ;null-terminate the reversed stringmov edx, offset reversedcall writestringcall crlf
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
intArray SWORD 0, 0, 0, 0, -1, 20, 35, -12, 66, 4, 0noneMsg BYTE "A non-zero value was not found", 0
mov ebx, OFFSET intArray ;point to the arraymov ecx, LENGTHOF intArray ;loop counterL1:
cmp WORD PTR [ebx], 0 ;compare value to zerojnz found ;found a valueadd ebx, TYPE intArray ;point to next
loop L1 ;continue the loopjmp notFound ;none foundfound: ;display the value
movsx eax, WORD PTR[ebx] ;sign-extend into EAXcall WriteIntjmp quit
notFound: ;display "not found" messagemov edx, OFFSET noneMsgcall WriteString
quit:call Crlf
exit
mov ebx, OFFSET intArray ;point to the arraymov ecx, LENGTHOF intArray ;loop counterL1:
cmp WORD PTR [ebx], 0 ;compare value to zerojnz found ;found a valueadd ebx, TYPE intArray ;point to next
loop L1 ;continue the loopjmp notFound ;none foundfound: ;display the value
movsx eax, WORD PTR[ebx] ;sign-extend into EAXcall WriteIntjmp quit
notFound: ;display "not found" messagemov edx, OFFSET noneMsgcall WriteString
quit:call Crlf
exit
MOVSX VS.MOVZX Real Example
The output will be -1
movsx eax, WORD PTR[ebx] ;sign-extend into EAXcall WriteInt
movzx eax, WORD PTR[ebx] ;zero-extend into EAXcall WriteInt
The output will be +65535 EAX = 0000FFFF
EAX = FFFFFFFF
MOVSX VS.MOVZX Real Example
The output will be 65535
movsx eax, WORD PTR[ebx] ;sign-extend into EAXcall WriteDec
movzx eax, WORD PTR[ebx] ;zero-extend into EAXcall WriteDec
The output will be 4294967295
EAX = 0000FFFF
EAX = FFFFFFFF
Assignment…
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.
Two Largest…Write an assembly program that takes 10 integers and
finds the two largest values among input integers
Questions?!
Thanks!