SlideShare uma empresa Scribd logo
1 de 49
Assembly Language (Lab 5)
Agenda
Revision
Conditional Loops
IF, While and Repeat Directives
Hands On
Data
Transfer
Inst.
MOV
MOVZX
/MOVSX
ADD/SUBINC/DEC
NEG
Tools
ReadDec/
ReadInt
Writestring
writeInt/
WriteDec
OFFSETLENGTHOF
TYPE
PTR
Addressing
Modes
Types
Register
Addressing
Immediate
Addressing
Direct
Memory
Addressing
In-Direct
Memory
Addressing
Direct
Offset
Addressing
Flags
Carry
Zero
Overflow
Sign
Parity
Auxiliary
Conditional Loop
LOOPE/LOOPZ and there counterpart …
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
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 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
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 On
Let’s make the best use of our time…
Min and Max in Array
Write 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 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
.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
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
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
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.)
New Directives
This problem can be done using CMP && JMP, but here
we will introduce new directives to do the same
functionality
.IF
.IF cond1
Statements
[.ELSEIF cond2
Statements]
[.ELSE
Statements]
.ENDIF
Boolean expressions
involving two
operands and a
conditional
operator within
them.
Possible operators
are ==, !=, >, >=,
<,
<=, && and ||
.IF
.data
uint1 dword 5
uint2 dword 10
int1 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, -1
JBE @C0001
mov eax, uint2
@C0001:
Generated Code:
CMP eax, int1
JLE @C0002
mov eax, uint2
@C0002:
VS
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.)
.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
.code
main PROC
mov edx, offset prompt
call writestring
call 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 crlf
exit
main ENDP
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.
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
main PROC
mov edx, offset prompt
call writestring
MOV 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 L1
JMP next
errorState:
MOV edx, OFFSET promptE
call writestring
call crlf
JMP endState
next:
MOV edx, OFFSET promptA
call writestring
MOV eax, CntA
call writeDec
call crlf
MOV edx, OFFSET promptB
call writestring
MOV eax, CntB
call writeDec
call crlf
MOV edx, OFFSET promptC
call writestring
MOV eax, CntC
call writeDec
call crlf
MOV edx, OFFSET promptD
call writestring
MOV eax, CntD
call writeDec
call crlf
MOV edx, OFFSET promptF
call writestring
MOV eax, CntF
call writeDec
call 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 & .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
.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
.code
main PROC
.REPEAT
mov edx, offset prompt
call writestring
call 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 stragain
call writestring
call readchar ;input char stored in AL register
.UNTIL al == 'N' || al == 'n‘
Reverse a String
Write an Assembly program that reverses a string
.data
original byte "I love Assembly :D", 0
reversed byte SIZEOF original dup(?)
.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
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, 0
noneMsg BYTE "A non-zero value was not found", 0
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
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
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
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
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!

Mais conteúdo relacionado

Mais procurados

Assembly Language Programming By Ytha Yu, Charles Marut Chap 8 (The Stack and...
Assembly Language Programming By Ytha Yu, Charles Marut Chap 8 (The Stack and...Assembly Language Programming By Ytha Yu, Charles Marut Chap 8 (The Stack and...
Assembly Language Programming By Ytha Yu, Charles Marut Chap 8 (The Stack and...Bilal Amjad
 
Ascii adjust & decimal adjust
Ascii adjust & decimal adjustAscii adjust & decimal adjust
Ascii adjust & decimal adjustTech_MX
 
Top 40 sql queries for testers
Top 40 sql queries for testersTop 40 sql queries for testers
Top 40 sql queries for testerstlvd
 
Assembly Language Programming By Ytha Yu, Charles Marut Chap 4 (Introduction ...
Assembly Language Programming By Ytha Yu, Charles Marut Chap 4 (Introduction ...Assembly Language Programming By Ytha Yu, Charles Marut Chap 4 (Introduction ...
Assembly Language Programming By Ytha Yu, Charles Marut Chap 4 (Introduction ...Bilal Amjad
 
knapsack problem
knapsack problemknapsack problem
knapsack problemAdnan Malak
 
Assembly language (coal)
Assembly language (coal)Assembly language (coal)
Assembly language (coal)Hareem Aslam
 
Jumps in Assembly Language.
Jumps in Assembly Language.Jumps in Assembly Language.
Jumps in Assembly Language.NA000000
 
SOP POS, Minterm and Maxterm
SOP POS, Minterm and MaxtermSOP POS, Minterm and Maxterm
SOP POS, Minterm and MaxtermSelf-employed
 
Unit 2 application of stack
Unit 2  application of stack Unit 2  application of stack
Unit 2 application of stack LavanyaJ28
 
Stack and its usage in assembly language
Stack and its usage in assembly language Stack and its usage in assembly language
Stack and its usage in assembly language Usman Bin Saad
 
Jmeter para Aplicaciones Web
Jmeter para Aplicaciones WebJmeter para Aplicaciones Web
Jmeter para Aplicaciones Webred.es
 
Logic Simplification Using SOP
Logic Simplification Using SOPLogic Simplification Using SOP
Logic Simplification Using SOPAbi Malik
 
Assembly Language Programming By Ytha Yu, Charles Marut Chap 6 (Flow Control ...
Assembly Language Programming By Ytha Yu, Charles Marut Chap 6 (Flow Control ...Assembly Language Programming By Ytha Yu, Charles Marut Chap 6 (Flow Control ...
Assembly Language Programming By Ytha Yu, Charles Marut Chap 6 (Flow Control ...Bilal Amjad
 

Mais procurados (20)

Assembly Language Programming By Ytha Yu, Charles Marut Chap 8 (The Stack and...
Assembly Language Programming By Ytha Yu, Charles Marut Chap 8 (The Stack and...Assembly Language Programming By Ytha Yu, Charles Marut Chap 8 (The Stack and...
Assembly Language Programming By Ytha Yu, Charles Marut Chap 8 (The Stack and...
 
Ascii adjust & decimal adjust
Ascii adjust & decimal adjustAscii adjust & decimal adjust
Ascii adjust & decimal adjust
 
Top 40 sql queries for testers
Top 40 sql queries for testersTop 40 sql queries for testers
Top 40 sql queries for testers
 
Assembly Language Programming By Ytha Yu, Charles Marut Chap 4 (Introduction ...
Assembly Language Programming By Ytha Yu, Charles Marut Chap 4 (Introduction ...Assembly Language Programming By Ytha Yu, Charles Marut Chap 4 (Introduction ...
Assembly Language Programming By Ytha Yu, Charles Marut Chap 4 (Introduction ...
 
knapsack problem
knapsack problemknapsack problem
knapsack problem
 
K map
K mapK map
K map
 
Assembly language (coal)
Assembly language (coal)Assembly language (coal)
Assembly language (coal)
 
Jumps in Assembly Language.
Jumps in Assembly Language.Jumps in Assembly Language.
Jumps in Assembly Language.
 
SOP POS, Minterm and Maxterm
SOP POS, Minterm and MaxtermSOP POS, Minterm and Maxterm
SOP POS, Minterm and Maxterm
 
Intro to assembly language
Intro to assembly languageIntro to assembly language
Intro to assembly language
 
Heaps & priority queues
Heaps & priority queuesHeaps & priority queues
Heaps & priority queues
 
Theory of computation Lec2
Theory of computation Lec2Theory of computation Lec2
Theory of computation Lec2
 
Unit 2 application of stack
Unit 2  application of stack Unit 2  application of stack
Unit 2 application of stack
 
stack presentation
stack presentationstack presentation
stack presentation
 
Memory Addressing
Memory AddressingMemory Addressing
Memory Addressing
 
Stack and its usage in assembly language
Stack and its usage in assembly language Stack and its usage in assembly language
Stack and its usage in assembly language
 
Jmeter para Aplicaciones Web
Jmeter para Aplicaciones WebJmeter para Aplicaciones Web
Jmeter para Aplicaciones Web
 
Logic Simplification Using SOP
Logic Simplification Using SOPLogic Simplification Using SOP
Logic Simplification Using SOP
 
Assembly Language Programming By Ytha Yu, Charles Marut Chap 6 (Flow Control ...
Assembly Language Programming By Ytha Yu, Charles Marut Chap 6 (Flow Control ...Assembly Language Programming By Ytha Yu, Charles Marut Chap 6 (Flow Control ...
Assembly Language Programming By Ytha Yu, Charles Marut Chap 6 (Flow Control ...
 
Assembly 8086
Assembly 8086Assembly 8086
Assembly 8086
 

Destaque

Assembly Language Tanka - SAKAI Hiroaki
Assembly Language Tanka - SAKAI HiroakiAssembly Language Tanka - SAKAI Hiroaki
Assembly Language Tanka - SAKAI Hiroakiasmtanka
 
Affective computing
Affective computingAffective computing
Affective computingAnkit Moonka
 
Assembly Language Lecture 4
Assembly Language Lecture 4Assembly Language Lecture 4
Assembly Language Lecture 4Motaz Saad
 
Part I:Introduction to assembly language
Part I:Introduction to assembly languagePart I:Introduction to assembly language
Part I:Introduction to assembly languageAhmed M. Abed
 
Assembly language programming_fundamentals 8086
Assembly language programming_fundamentals 8086Assembly language programming_fundamentals 8086
Assembly language programming_fundamentals 8086Shehrevar Davierwala
 
Assembly Language Lecture 1
Assembly Language Lecture 1Assembly Language Lecture 1
Assembly Language Lecture 1Motaz Saad
 

Destaque (13)

biodata
biodatabiodata
biodata
 
[ASM]Lab3
[ASM]Lab3[ASM]Lab3
[ASM]Lab3
 
[ASM] Lab2
[ASM] Lab2[ASM] Lab2
[ASM] Lab2
 
Assembly fundamentals
Assembly fundamentalsAssembly fundamentals
Assembly fundamentals
 
Assembly Language Tanka - SAKAI Hiroaki
Assembly Language Tanka - SAKAI HiroakiAssembly Language Tanka - SAKAI Hiroaki
Assembly Language Tanka - SAKAI Hiroaki
 
Affective computing
Affective computingAffective computing
Affective computing
 
[ASM]Lab4
[ASM]Lab4[ASM]Lab4
[ASM]Lab4
 
[ASM] Lab1
[ASM] Lab1[ASM] Lab1
[ASM] Lab1
 
Assembly Language Lecture 4
Assembly Language Lecture 4Assembly Language Lecture 4
Assembly Language Lecture 4
 
8086 microprocessor lab manual
8086 microprocessor lab manual8086 microprocessor lab manual
8086 microprocessor lab manual
 
Part I:Introduction to assembly language
Part I:Introduction to assembly languagePart I:Introduction to assembly language
Part I:Introduction to assembly language
 
Assembly language programming_fundamentals 8086
Assembly language programming_fundamentals 8086Assembly language programming_fundamentals 8086
Assembly language programming_fundamentals 8086
 
Assembly Language Lecture 1
Assembly Language Lecture 1Assembly Language Lecture 1
Assembly Language Lecture 1
 

Semelhante a [ASM]Lab5

Emulador de ensamblador EMU8086
Emulador de ensamblador EMU8086Emulador de ensamblador EMU8086
Emulador de ensamblador EMU8086Jhon Alexito
 
Conditional Control in MATLAB Scripts
Conditional Control in MATLAB ScriptsConditional Control in MATLAB Scripts
Conditional Control in MATLAB ScriptsShameer Ahmed Koya
 
Loop instruction, controlling the flow of progam
Loop instruction, controlling the flow of progamLoop instruction, controlling the flow of progam
Loop instruction, controlling the flow of progamDr. Girish GS
 
Lenguaje ensamblador EMU8086
Lenguaje ensamblador EMU8086Lenguaje ensamblador EMU8086
Lenguaje ensamblador EMU8086Santy Bolo
 
Python programming workshop session 2
Python programming workshop session 2Python programming workshop session 2
Python programming workshop session 2Abdul Haseeb
 
Compiladoresemulador
CompiladoresemuladorCompiladoresemulador
CompiladoresemuladorDavid Caicedo
 
Chapter 6 Flow control Instructions
Chapter 6 Flow control InstructionsChapter 6 Flow control Instructions
Chapter 6 Flow control Instructionswarda aziz
 
Write an MASM program that meets the following requirements- Please us.pdf
Write an MASM program that meets the following requirements- Please us.pdfWrite an MASM program that meets the following requirements- Please us.pdf
Write an MASM program that meets the following requirements- Please us.pdfasarts99
 
Reversing malware analysis training part4 assembly programming basics
Reversing malware analysis training part4 assembly programming basicsReversing malware analysis training part4 assembly programming basics
Reversing malware analysis training part4 assembly programming basicsCysinfo Cyber Security Community
 
3-Conditional-if-else-switch btech computer in c.pdf
3-Conditional-if-else-switch btech computer in c.pdf3-Conditional-if-else-switch btech computer in c.pdf
3-Conditional-if-else-switch btech computer in c.pdfArkSingh7
 

Semelhante a [ASM]Lab5 (20)

Chapt 06
Chapt 06Chapt 06
Chapt 06
 
Chapt 06
Chapt 06Chapt 06
Chapt 06
 
Emulador de ensamblador EMU8086
Emulador de ensamblador EMU8086Emulador de ensamblador EMU8086
Emulador de ensamblador EMU8086
 
Conditional Control in MATLAB Scripts
Conditional Control in MATLAB ScriptsConditional Control in MATLAB Scripts
Conditional Control in MATLAB Scripts
 
Loop instruction, controlling the flow of progam
Loop instruction, controlling the flow of progamLoop instruction, controlling the flow of progam
Loop instruction, controlling the flow of progam
 
Lenguaje ensamblador EMU8086
Lenguaje ensamblador EMU8086Lenguaje ensamblador EMU8086
Lenguaje ensamblador EMU8086
 
Python programming workshop session 2
Python programming workshop session 2Python programming workshop session 2
Python programming workshop session 2
 
Compiladoresemulador
CompiladoresemuladorCompiladoresemulador
Compiladoresemulador
 
Chapter 6 Flow control Instructions
Chapter 6 Flow control InstructionsChapter 6 Flow control Instructions
Chapter 6 Flow control Instructions
 
X86 operation types
X86 operation typesX86 operation types
X86 operation types
 
Write an MASM program that meets the following requirements- Please us.pdf
Write an MASM program that meets the following requirements- Please us.pdfWrite an MASM program that meets the following requirements- Please us.pdf
Write an MASM program that meets the following requirements- Please us.pdf
 
Al2ed chapter7
Al2ed chapter7Al2ed chapter7
Al2ed chapter7
 
loops in C ppt.pdf
loops in C ppt.pdfloops in C ppt.pdf
loops in C ppt.pdf
 
Python for Beginners(v2)
Python for Beginners(v2)Python for Beginners(v2)
Python for Beginners(v2)
 
Loops c++
Loops c++Loops c++
Loops c++
 
Al2ed chapter8
Al2ed chapter8Al2ed chapter8
Al2ed chapter8
 
Reversing malware analysis training part4 assembly programming basics
Reversing malware analysis training part4 assembly programming basicsReversing malware analysis training part4 assembly programming basics
Reversing malware analysis training part4 assembly programming basics
 
Lecture no 15
Lecture no 15Lecture no 15
Lecture no 15
 
[ASM]Lab8
[ASM]Lab8[ASM]Lab8
[ASM]Lab8
 
3-Conditional-if-else-switch btech computer in c.pdf
3-Conditional-if-else-switch btech computer in c.pdf3-Conditional-if-else-switch btech computer in c.pdf
3-Conditional-if-else-switch btech computer in c.pdf
 

Mais de Nora Youssef

Mais de Nora Youssef (11)

Welcome to dragons’ land
Welcome to dragons’ landWelcome to dragons’ land
Welcome to dragons’ land
 
[SpLab1]Review
[SpLab1]Review[SpLab1]Review
[SpLab1]Review
 
[SpLab2]Arrays
[SpLab2]Arrays[SpLab2]Arrays
[SpLab2]Arrays
 
[SpLab3]Structures
[SpLab3]Structures[SpLab3]Structures
[SpLab3]Structures
 
[SpLab5] Pointers II
[SpLab5] Pointers II[SpLab5] Pointers II
[SpLab5] Pointers II
 
[SpLab4]Pointers I
[SpLab4]Pointers I[SpLab4]Pointers I
[SpLab4]Pointers I
 
[SpLab7]Functions I
[SpLab7]Functions I[SpLab7]Functions I
[SpLab7]Functions I
 
[Splab8]Functions II
[Splab8]Functions II[Splab8]Functions II
[Splab8]Functions II
 
[SpLab9]Functions III
[SpLab9]Functions III[SpLab9]Functions III
[SpLab9]Functions III
 
[ASM]Lab7
[ASM]Lab7[ASM]Lab7
[ASM]Lab7
 
Abstract
AbstractAbstract
Abstract
 

Último

Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxnegromaestrong
 
psychiatric nursing HISTORY COLLECTION .docx
psychiatric  nursing HISTORY  COLLECTION  .docxpsychiatric  nursing HISTORY  COLLECTION  .docx
psychiatric nursing HISTORY COLLECTION .docxPoojaSen20
 
fourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writingfourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writingTeacherCyreneCayanan
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxAreebaZafar22
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.pptRamjanShidvankar
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactdawncurless
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactPECB
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxheathfieldcps1
 
Making and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdfMaking and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdfChris Hunter
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfJayanti Pande
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin ClassesCeline George
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphThiyagu K
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsTechSoup
 
An Overview of Mutual Funds Bcom Project.pdf
An Overview of Mutual Funds Bcom Project.pdfAn Overview of Mutual Funds Bcom Project.pdf
An Overview of Mutual Funds Bcom Project.pdfSanaAli374401
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdfQucHHunhnh
 
SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...
SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...
SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...KokoStevan
 
Class 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfClass 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfAyushMahapatra5
 

Último (20)

Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptx
 
psychiatric nursing HISTORY COLLECTION .docx
psychiatric  nursing HISTORY  COLLECTION  .docxpsychiatric  nursing HISTORY  COLLECTION  .docx
psychiatric nursing HISTORY COLLECTION .docx
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
 
fourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writingfourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writing
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
Making and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdfMaking and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdf
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdf
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot Graph
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
An Overview of Mutual Funds Bcom Project.pdf
An Overview of Mutual Funds Bcom Project.pdfAn Overview of Mutual Funds Bcom Project.pdf
An Overview of Mutual Funds Bcom Project.pdf
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...
SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...
SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...
 
Class 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfClass 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdf
 

[ASM]Lab5

  • 2. Agenda Revision Conditional Loops IF, While and Repeat Directives Hands On
  • 7. Conditional Loop LOOPE/LOOPZ and there counterpart …
  • 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
  • 12. Hands On Let’s make the best use of our time…
  • 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
  • 20. .IF .IF cond1 Statements [.ELSEIF cond2 Statements] [.ELSE Statements] .ENDIF Boolean expressions involving two operands and a conditional operator within them. Possible operators are ==, !=, >, >=, <, <=, && and ||
  • 21. .IF .data uint1 dword 5 uint2 dword 10 int1 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, -1 JBE @C0001 mov eax, uint2 @C0001: Generated Code: CMP eax, int1 JLE @C0002 mov eax, uint2 @C0002: VS
  • 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.)
  • 23. .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
  • 24. .code main PROC mov edx, offset prompt call writestring call 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
  • 25. call writestring ;print the output msg based on the prev selection call crlf exit main ENDP
  • 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
  • 28. main PROC mov edx, offset prompt call writestring MOV 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 L1 JMP next
  • 29. errorState: MOV edx, OFFSET promptE call writestring call crlf JMP endState next: MOV edx, OFFSET promptA call writestring MOV eax, CntA call writeDec call crlf
  • 30. MOV edx, OFFSET promptB call writestring MOV eax, CntB call writeDec call crlf MOV edx, OFFSET promptC call writestring MOV eax, CntC call writeDec call crlf
  • 31. MOV edx, OFFSET promptD call writestring MOV eax, CntD call writeDec call crlf MOV edx, OFFSET promptF call writestring MOV eax, CntF call writeDec call crlf endState: exit
  • 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
  • 35. .code main PROC .REPEAT mov edx, offset prompt call writestring call 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 stragain call writestring call readchar ;input char stored in AL register .UNTIL al == 'N' || al == 'n‘
  • 36. Reverse a String Write an Assembly program that reverses a string
  • 37. .data original byte "I love Assembly :D", 0 reversed byte SIZEOF original dup(?)
  • 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

Notas do Editor

  1. 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
  2. .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.
  3. 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
  4. 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