49
Assembly Language (Lab 5)

[ASM]Lab5

Embed Size (px)

Citation preview

Page 1: [ASM]Lab5

Assembly Language (Lab 5)

Page 2: [ASM]Lab5

AgendaRevisionConditional LoopsIF, While and Repeat DirectivesHands On

Page 3: [ASM]Lab5

Data Transfer

Inst.

MOV

MOVZX/MOVSX

ADD/SUB

INC/DEC

NEG

Page 4: [ASM]Lab5

Tools

ReadDec/ReadInt

Writestring

writeInt/WriteDec

OFFSET

LENGTHOF

TYPE

PTR

Page 5: [ASM]Lab5

Addressing Modes

Types

Register Addressin

g

Immediate Addressin

g

Direct Memory

Addressing

In-Direct Memory

Addressing

Direct Offset

Addressing

Page 6: [ASM]Lab5

Flags

Carry

Zero

Overflow

Sign

Parity

Auxiliary

Page 7: [ASM]Lab5

Conditional LoopLOOPE/LOOPZ and there counterpart …

Page 8: [ASM]Lab5

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

Page 9: [ASM]Lab5

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

Page 10: [ASM]Lab5

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

Page 11: [ASM]Lab5

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

Page 12: [ASM]Lab5

Hands OnLet’s make the best use of our time…

Page 13: [ASM]Lab5

Min and Max in ArrayWrite an assembly program that finds the min and max of

an entered integer array.

Page 14: [ASM]Lab5

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

Page 15: [ASM]Lab5

.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

Page 16: [ASM]Lab5

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

Page 17: [ASM]Lab5

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

Page 18: [ASM]Lab5

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.)

Page 19: [ASM]Lab5

New Directives This problem can be done using CMP && JMP, but here

we will introduce new directives to do the same functionality

Page 20: [ASM]Lab5

.IF

.IF cond1Statements[.ELSEIF cond2Statements][.ELSEStatements].ENDIF

Boolean expressions involving two

operands and a conditional

operator within them.

Possible operators are ==, !=, >, >=,

<,<=, && and ||

Page 21: [ASM]Lab5

.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

Page 22: [ASM]Lab5

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.)

Page 23: [ASM]Lab5

.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

Page 24: [ASM]Lab5

.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

Page 25: [ASM]Lab5

call writestring ;print the output msg based on the prev selection

call crlfexitmain ENDP

Page 26: [ASM]Lab5

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.

Page 27: [ASM]Lab5

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

Page 28: [ASM]Lab5

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

Page 29: [ASM]Lab5

errorState:MOV edx, OFFSET promptEcall writestring call crlfJMP endState

next:MOV edx, OFFSET promptAcall writestring MOV eax, CntAcall writeDeccall crlf

Page 30: [ASM]Lab5

MOV edx, OFFSET promptBcall writestringMOV eax, CntBcall writeDeccall crlf

MOV edx, OFFSET promptCcall writestring MOV eax, CntCcall writeDeccall crlf

Page 31: [ASM]Lab5

MOV edx, OFFSET promptDcall writestringMOV eax, CntDcall writeDeccall crlf

MOV edx, OFFSET promptFcall writestring MOV eax, CntFcall writeDeccall crlf

endState:exit

Page 32: [ASM]Lab5

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

Page 33: [ASM]Lab5

.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

Page 34: [ASM]Lab5

.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

Page 35: [ASM]Lab5

.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‘

Page 36: [ASM]Lab5

Reverse a StringWrite an Assembly program that reverses a string

Page 37: [ASM]Lab5

.dataoriginal byte "I love Assembly :D", 0reversed byte SIZEOF original dup(?)

Page 38: [ASM]Lab5

.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

Page 39: [ASM]Lab5

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

Page 40: [ASM]Lab5

intArray SWORD 0, 0, 0, 0, -1, 20, 35, -12, 66, 4, 0noneMsg BYTE "A non-zero value was not found", 0

Page 41: [ASM]Lab5

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

Page 42: [ASM]Lab5

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

Page 43: [ASM]Lab5

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

Page 44: [ASM]Lab5

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

Page 45: [ASM]Lab5

Assignment…

Page 46: [ASM]Lab5

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.

Page 47: [ASM]Lab5

Two Largest…Write an assembly program that takes 10 integers and

finds the two largest values among input integers

Page 48: [ASM]Lab5

Questions?!

Page 49: [ASM]Lab5

Thanks!