SlideShare a Scribd company logo
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!

More Related Content

What's hot

1D Array in Assembly Language
1D Array in Assembly Language1D Array in Assembly Language
1D Array in Assembly Language
Javeria Yaqoob
 
Representation of numbers and characters
Representation of numbers and charactersRepresentation of numbers and characters
Representation of numbers and characters
warda aziz
 
Regular expressions
Regular expressionsRegular expressions
Regular expressions
Shiraz316
 
Lecture 1,2
Lecture 1,2Lecture 1,2
Lecture 1,2
shah zeb
 
TOC 7 | CFG in Chomsky Normal Form
TOC 7 | CFG in Chomsky Normal FormTOC 7 | CFG in Chomsky Normal Form
TOC 7 | CFG in Chomsky Normal Form
Mohammad Imam Hossain
 
File handling in C
File handling in CFile handling in C
File handling in C
Kamal Acharya
 
assembly language programming and organization of IBM PC" by YTHA YU
assembly language programming and organization of IBM PC" by YTHA YUassembly language programming and organization of IBM PC" by YTHA YU
assembly language programming and organization of IBM PC" by YTHA YU
Education
 
1327 Addressing Modes Of 8086
1327 Addressing Modes Of 80861327 Addressing Modes Of 8086
1327 Addressing Modes Of 8086
techbed
 
Peterson Critical Section Problem Solution
Peterson Critical Section Problem SolutionPeterson Critical Section Problem Solution
Peterson Critical Section Problem Solution
Bipul Chandra Kar
 
Automata theory - CFG and normal forms
Automata theory - CFG and normal formsAutomata theory - CFG and normal forms
Automata theory - CFG and normal forms
Akila Krishnamoorthy
 
Assembly Language Lecture 3
Assembly Language Lecture 3Assembly Language Lecture 3
Assembly Language Lecture 3
Motaz Saad
 
Recognition-of-tokens
Recognition-of-tokensRecognition-of-tokens
Recognition-of-tokens
Dattatray Gandhmal
 
Deterministic Finite Automata
Deterministic Finite AutomataDeterministic Finite Automata
Deterministic Finite Automata
Shiraz316
 
Unit 2 assembly language programming
Unit 2   assembly language programmingUnit 2   assembly language programming
Unit 2 assembly language programming
Kartik Sharma
 
bubble sorting of an array in 8086 assembly language
bubble sorting of an array in 8086 assembly languagebubble sorting of an array in 8086 assembly language
bubble sorting of an array in 8086 assembly language
Bilal Amjad
 
TOC 9 | Pushdown Automata
TOC 9 | Pushdown AutomataTOC 9 | Pushdown Automata
TOC 9 | Pushdown Automata
Mohammad Imam Hossain
 
Ch 2 lattice & boolean algebra
Ch 2 lattice & boolean algebraCh 2 lattice & boolean algebra
Ch 2 lattice & boolean algebra
Rupali Rana
 
TOC 2 | Deterministic Finite Automata
TOC 2 | Deterministic Finite AutomataTOC 2 | Deterministic Finite Automata
TOC 2 | Deterministic Finite Automata
Mohammad Imam Hossain
 
Moore and mealy machines
Moore and mealy machinesMoore and mealy machines
Moore and mealy machineslavishka_anuj
 
Memory Addressing
Memory AddressingMemory Addressing
Memory Addressing
chauhankapil
 

What's hot (20)

1D Array in Assembly Language
1D Array in Assembly Language1D Array in Assembly Language
1D Array in Assembly Language
 
Representation of numbers and characters
Representation of numbers and charactersRepresentation of numbers and characters
Representation of numbers and characters
 
Regular expressions
Regular expressionsRegular expressions
Regular expressions
 
Lecture 1,2
Lecture 1,2Lecture 1,2
Lecture 1,2
 
TOC 7 | CFG in Chomsky Normal Form
TOC 7 | CFG in Chomsky Normal FormTOC 7 | CFG in Chomsky Normal Form
TOC 7 | CFG in Chomsky Normal Form
 
File handling in C
File handling in CFile handling in C
File handling in C
 
assembly language programming and organization of IBM PC" by YTHA YU
assembly language programming and organization of IBM PC" by YTHA YUassembly language programming and organization of IBM PC" by YTHA YU
assembly language programming and organization of IBM PC" by YTHA YU
 
1327 Addressing Modes Of 8086
1327 Addressing Modes Of 80861327 Addressing Modes Of 8086
1327 Addressing Modes Of 8086
 
Peterson Critical Section Problem Solution
Peterson Critical Section Problem SolutionPeterson Critical Section Problem Solution
Peterson Critical Section Problem Solution
 
Automata theory - CFG and normal forms
Automata theory - CFG and normal formsAutomata theory - CFG and normal forms
Automata theory - CFG and normal forms
 
Assembly Language Lecture 3
Assembly Language Lecture 3Assembly Language Lecture 3
Assembly Language Lecture 3
 
Recognition-of-tokens
Recognition-of-tokensRecognition-of-tokens
Recognition-of-tokens
 
Deterministic Finite Automata
Deterministic Finite AutomataDeterministic Finite Automata
Deterministic Finite Automata
 
Unit 2 assembly language programming
Unit 2   assembly language programmingUnit 2   assembly language programming
Unit 2 assembly language programming
 
bubble sorting of an array in 8086 assembly language
bubble sorting of an array in 8086 assembly languagebubble sorting of an array in 8086 assembly language
bubble sorting of an array in 8086 assembly language
 
TOC 9 | Pushdown Automata
TOC 9 | Pushdown AutomataTOC 9 | Pushdown Automata
TOC 9 | Pushdown Automata
 
Ch 2 lattice & boolean algebra
Ch 2 lattice & boolean algebraCh 2 lattice & boolean algebra
Ch 2 lattice & boolean algebra
 
TOC 2 | Deterministic Finite Automata
TOC 2 | Deterministic Finite AutomataTOC 2 | Deterministic Finite Automata
TOC 2 | Deterministic Finite Automata
 
Moore and mealy machines
Moore and mealy machinesMoore and mealy machines
Moore and mealy machines
 
Memory Addressing
Memory AddressingMemory Addressing
Memory Addressing
 

Viewers also liked

[ASM]Lab3
[ASM]Lab3[ASM]Lab3
[ASM]Lab3
Nora Youssef
 
[ASM] Lab2
[ASM] Lab2[ASM] Lab2
[ASM] Lab2
Nora Youssef
 
Assembly fundamentals
Assembly fundamentalsAssembly fundamentals
Assembly fundamentals
Syed Zaid Irshad
 
Assembly Language Tanka - SAKAI Hiroaki
Assembly Language Tanka - SAKAI HiroakiAssembly Language Tanka - SAKAI Hiroaki
Assembly Language Tanka - SAKAI Hiroaki
asmtanka
 
Affective computing
Affective computingAffective computing
Affective computingAnkit Moonka
 
[ASM]Lab4
[ASM]Lab4[ASM]Lab4
[ASM]Lab4
Nora Youssef
 
[ASM] Lab1
[ASM] Lab1[ASM] Lab1
[ASM] Lab1
Nora Youssef
 
Assembly Language Lecture 4
Assembly Language Lecture 4Assembly Language Lecture 4
Assembly Language Lecture 4
Motaz 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 8086
Shehrevar Davierwala
 
Assembly Language Lecture 1
Assembly Language Lecture 1Assembly Language Lecture 1
Assembly Language Lecture 1
Motaz Saad
 

Viewers also liked (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
 

Similar to [ASM]Lab5

Emulador de ensamblador EMU8086
Emulador de ensamblador EMU8086Emulador de ensamblador EMU8086
Emulador de ensamblador EMU8086
Jhon Alexito
 
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
 
Conditional Control in MATLAB Scripts
Conditional Control in MATLAB ScriptsConditional Control in MATLAB Scripts
Conditional Control in MATLAB Scripts
Shameer Ahmed Koya
 
Assembly Language Lecture 5
Assembly Language Lecture 5Assembly Language Lecture 5
Assembly Language Lecture 5
Motaz Saad
 
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
Dr. Girish GS
 
Lenguaje ensamblador EMU8086
Lenguaje ensamblador EMU8086Lenguaje ensamblador EMU8086
Lenguaje ensamblador EMU8086
Santy Bolo
 
Python programming workshop session 2
Python programming workshop session 2Python programming workshop session 2
Python programming workshop session 2
Abdul Haseeb
 
Compiladoresemulador
CompiladoresemuladorCompiladoresemulador
Compiladoresemulador
David Caicedo
 
X86 operation types
X86 operation typesX86 operation types
X86 operation types
Rowena Cornejo
 
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
asarts99
 
loops in C ppt.pdf
loops in C ppt.pdfloops in C ppt.pdf
loops in C ppt.pdf
DrSamsonChepuri1
 
Python for Beginners(v2)
Python for Beginners(v2)Python for Beginners(v2)
Python for Beginners(v2)
Panimalar Engineering College
 
Loops c++
Loops c++Loops c++
Loops c++
Shivani Singh
 
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
Cysinfo Cyber Security Community
 
Lecture no 15
Lecture no 15Lecture no 15
Lecture no 15
zaidshaidzaid
 
[ASM]Lab8
[ASM]Lab8[ASM]Lab8
[ASM]Lab8
Nora Youssef
 

Similar to [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
 
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 ...
 
Conditional Control in MATLAB Scripts
Conditional Control in MATLAB ScriptsConditional Control in MATLAB Scripts
Conditional Control in MATLAB Scripts
 
Assembly Language Lecture 5
Assembly Language Lecture 5Assembly Language Lecture 5
Assembly Language Lecture 5
 
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
 
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
 

More from Nora Youssef

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

More from Nora Youssef (12)

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
 
[ASM]Lab6
[ASM]Lab6[ASM]Lab6
[ASM]Lab6
 
Abstract
AbstractAbstract
Abstract
 

Recently uploaded

Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdfAdversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
Po-Chuan Chen
 
Unit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdfUnit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdf
Thiyagu K
 
Language Across the Curriculm LAC B.Ed.
Language Across the  Curriculm LAC B.Ed.Language Across the  Curriculm LAC B.Ed.
Language Across the Curriculm LAC B.Ed.
Atul Kumar Singh
 
The Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdfThe Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdf
kaushalkr1407
 
Lapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdfLapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdf
Jean Carlos Nunes Paixão
 
How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17
Celine George
 
Guidance_and_Counselling.pdf B.Ed. 4th Semester
Guidance_and_Counselling.pdf B.Ed. 4th SemesterGuidance_and_Counselling.pdf B.Ed. 4th Semester
Guidance_and_Counselling.pdf B.Ed. 4th Semester
Atul Kumar Singh
 
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
Nguyen Thanh Tu Collection
 
Overview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with MechanismOverview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with Mechanism
DeeptiGupta154
 
CACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdfCACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdf
camakaiclarkmusic
 
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdfUnit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Thiyagu K
 
Digital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and ResearchDigital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and Research
Vikramjit Singh
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
siemaillard
 
special B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdfspecial B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdf
Special education needs
 
Francesca Gottschalk - How can education support child empowerment.pptx
Francesca Gottschalk - How can education support child empowerment.pptxFrancesca Gottschalk - How can education support child empowerment.pptx
Francesca Gottschalk - How can education support child empowerment.pptx
EduSkills OECD
 
Thesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.pptThesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.ppt
EverAndrsGuerraGuerr
 
A Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in EducationA Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in Education
Peter Windle
 
Synthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptxSynthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptx
Pavel ( NSTU)
 
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
EugeneSaldivar
 
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCECLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
BhavyaRajput3
 

Recently uploaded (20)

Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdfAdversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
 
Unit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdfUnit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdf
 
Language Across the Curriculm LAC B.Ed.
Language Across the  Curriculm LAC B.Ed.Language Across the  Curriculm LAC B.Ed.
Language Across the Curriculm LAC B.Ed.
 
The Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdfThe Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdf
 
Lapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdfLapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdf
 
How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17
 
Guidance_and_Counselling.pdf B.Ed. 4th Semester
Guidance_and_Counselling.pdf B.Ed. 4th SemesterGuidance_and_Counselling.pdf B.Ed. 4th Semester
Guidance_and_Counselling.pdf B.Ed. 4th Semester
 
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
 
Overview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with MechanismOverview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with Mechanism
 
CACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdfCACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdf
 
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdfUnit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdf
 
Digital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and ResearchDigital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and Research
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
 
special B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdfspecial B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdf
 
Francesca Gottschalk - How can education support child empowerment.pptx
Francesca Gottschalk - How can education support child empowerment.pptxFrancesca Gottschalk - How can education support child empowerment.pptx
Francesca Gottschalk - How can education support child empowerment.pptx
 
Thesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.pptThesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.ppt
 
A Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in EducationA Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in Education
 
Synthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptxSynthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptx
 
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
 
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCECLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
 

[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

Editor's Notes

  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