Lab5_Slidesx
Download
Report
Transcript Lab5_Slidesx
Agenda
• Lab 4 cont:
•
•
•
•
•
Data-Related Operators and Directives
Boolean and comparison instructions
Conditional JMP
LOOP
Conditional LOOP
• MASM directives
• Hands On
• Assignment
Data-Related Operators and Directives
You can use a number of MASM directives to
get information about the addresses and size
characteristics of data:
• OFFSET
• PTR
• TYPE
• LengthOf
• SizeOf
OFFSET reserved word is an operator that retrieves the offset
address of the given variable.
OFFSET
The offset represents the distance, in bytes, of the label from
the beginning of the data segment
You can use the PTR operator to override the declared size of
an operand
PTR
This is only necessary when you’re trying to access the variable
using a size attribute that’s different from the one used to
declare the variable.
TYPE
The TYPE operator returns the size, in bytes, of a single
element of a variable
LENGTHOF
The LENGTHOF operator counts the number of elements in an
array
SIZEOF
The SIZEOF operator returns a value that is equivalent to
multiplying LENGTHOF by TYPE.
OFFSET Operator
INCLUDE Irvine32.inc
.data
bVal
wVal
dVal
dVal2
BYTE
WORD
DWORD
DWORD
.code
main PROC
mov esi,
mov esi,
mov esi,
mov esi,
exit
main ENDP
END main
?
?
?
?
OFFSET
OFFSET
OFFSET
OFFSET
;bVal located at offset 00404000h
bVal
wVal
dVal
dVal2
;
;
;
;
ESI
ESI
ESI
ESI
=
=
=
=
00404000
00404001
00404003
00404007
PTR Operator
Move the lower 16 bits of a doubleword variable
named myDouble into AX.
.data
myDouble DWORD 12345678h
.code
mov ax,myDouble ; error
mov ax,WORD PTR myDouble
TYPE Operator
.data
var1 BYTE ?
var2 WORD ?
var3 DWORD ?
var4 QWORD ?
.code
mov al,
mov al,
mov al,
mov al,
type
type
type
type
var1
var2
var3
var4
;
;
;
;
al
al
al
al
=
=
=
=
01h
02h
04h
08h
LengthOf & SizeOf Operators
.data
array word 1000h, 2000h, 3000h
.code
mov al, lengthOf array
; al = 03h
mov al, sizeOf
array
; al = 06h
AND Instruction
• The AND instruction performs a boolean (bitwise)
AND operation between each pair of matching bits in
two operands and places the result in the destination
operand.
• The AND instruction lets you clear one or more bits
in an operand without affecting other bits.
AND destination, source
mov al, 10101111b
and al, 11110110b
; al = 10101111b
; al = 10100110b
OR Instruction
• The OR instruction performs a boolean (bitwise)
OR operation between each pair of matching bits
in two operands and places the result in the
destination operand.
• The OR instruction lets you set one or more
bits in an operand without affecting other bits.
OR destination, source
mov al, 11100011b
or al, 00000100b
; al = 11100011b
; al = 11100111b
XOR Instruction
• The XOR instruction performs a boolean (bitwise)
XOR operation between each pair of matching bits
in two operands and places the result in the
destination operand.
XOR destination, source
NOT Instruction
• The NOT instruction toggles (inverts) all bits in an
operand.
• The result is called the one’s complement.
NOT reg
NOT mem
TEST Instruction
• The TEST instruction performs an implied AND operation
between each pair of matching bits in two operands.
• TEST doesn’t modify the destination operand, but it sets the
Sign, Zero and Parity flags.
• TEST is particularly valuable for finding out whether
individual bits in an operand are set.
TEST destination, Source
TEST Instruction
• Suppose we want to know whether bit 0 or bit 3 is set
in the AL register.
mov al, 00100101b
test al, 00001001b
; Result = 00000001b, ZF = 0
mov al, 00100100b
test al, 00001001b
; Result = 00000000b, ZF = 1
• We can infer that the Zero flag is set only when all
tested bits are clear.
CMP Instruction
• The CMP instruction compares the destination
operand to the source operand.
• It performs an implied subtraction of a source
operand from a destination operand.
• Neither operand is modified.
• CMP is a valuable tool for creating conditional logic
structures.
CMP destination, Source
CMP Instruction
• When two unsigned operands are compared,
the Zero and Carry flags indicate the
following relations between operands.
CMP Results
ZF
CF
Destination < source
0
1
Destination > source
0
0
Destination = source
1
0
CMP Instruction
mov ax, 5
cmp ax, 10
ZF = 0, CF = 1
mov ax, 1000
mov cx, 1000
cmp cx, ax
ZF = 1, CF = 0
mov si, 105
cmp si, 0
ZF = 0, CF = 0
CMP Instruction
• When two signed operands are compared, the
Sign, Zero and Overflow flags indicate the
following relations between operands.
CMP Results
Flags
Destination < source
SF ≠ OF
Destination > source
SF = OF
Destination = source
ZF = 1
JMP and LOOP Instructions
• By default, the CPU loads and executes programs
sequentially.
• But the current instruction might be conditional,
meaning that it transfers control to a new location
in the program based on the values of CPU status
flags (Zero, Sign, Carry, etc.).
Assembly language
programs use conditional
instructions to implement
high-level statements such
as IF statements and loops
A transfer of control
(jump), or branch, is a
way of altering the
order in which
statements are executed
JMP Instruction
• The JMP instruction causes an unconditional
transfer to a destination, identified by a code label
that is translated by the assembler into an offset
(address).
JMP destination
• When the CPU executes an unconditional transfer,
the offset of destination is moved into the
instruction pointer (EIP), causing execution to
continue at the new location.
JMP Instruction
• The JMP instruction provides an easy way to create a
loop by jumping to a label at the top of the loop:
top:
…
…
…
…
…
jmp top
; repeat the endless loop
• JMP is unconditional, so a loop like this will continue
endlessly unless another way is found to exit the loop.
Conditional Structures
Two Steps are involved in executing a
conditional statement
Comparison
An operation such as
CMP, AND or SUB
modifies the CPU
status flags
Jump
Conditional jump
instruction tests the
flags and causes a
branch to a new
address
Conditional Jump Instructions
• A conditional jump instruction branches to a
destination label when a status flag condition is true.
• If the flag condition is false, the instruction
immediately following the conditional jump is
executed.
Conditional Jump Instructions
The following table shows the conditional Jump
instructions.
Relation
For Unsigned
Data
For Signed Data
Equal/Zero
JE/JZ
Not Equal/ Not Zero
JNE/ JNZ
Above/ Greater
JA/JNBE
JG/JNLE
Above or Equal/
Greater or Equal
JAE/JNB
JGE/JNL
Below/ Less
JB/JNAE
JL/JNGE
Below or Equal/
Less or Equal
JBE/JNA
JLE/JNG
Hands On
• Write an Assembly code that takes two unsigned
integers X & Y from the user and prints the relation
between those integers:
• X is Above Y
• X is Below Y
• X is Equal Y
INCLUDE Irvine32.inc
.data
strAbove byte "X is above Y", 0
strBelow byte "X is below Y", 0
strEqual byte "X is equal Y", 0
x dword ?
y dword ?
.code
main PROC
call ReadDec
mov x, eax
call ReadDec
mov y, eax
cmp x, eax ;eax still has y value
ja above
jb below
je equal
above:
;handle above case
mov edx, offset strAbove
call writesSring
jmp next
below:
;handle below case
mov edx, offset strBelow
call writeString
jmp next
equal:
;handle equal case
mov edx, offset strEqual
call writeString
next:
call CrLf
exit
main ENDP
END main
Statement
IF-ELSE
IF
While Loop
In High level language
If ( i == j )
{
X = 10;
}
else
{
Y = 10;
}
If ( i > j )
{
X = 5;
}
Y = 5;
While ( i > j )
{
x += i;
i--;
}
In Assembly
mov eax, i
cmp eax, j
jne else
mov X, 10
jmp endif
else:
Mov Y, 10
endif:
mov eax, i
moveax,
eax,j i
cmp
Jna
endif
while:
mov cmp
X, 5 eax, j
endif:
jna endwhile
Y, 5
addmov
x, eax
dec eax
jmp while
endwhile:
mov i, eax
LOOP Instruction
• The LOOP instruction, formally known as Loop
According to ECX Counter, repeats a block of
statements a specific number of times.
• ECX is automatically used as a counter and is
decremented each time the loop repeats.
LOOP destination
LOOP Instruction
The execution of the LOOP instruction
involves two steps:
It subtracts 1 from
ECX
If ECX is not equal
to zero, a jump is
taken to the label
identified by
destination
It compares ECX to
zero
If ECX equals zero,
no jump takes place,
and control passes to
the instruction
following the loop
LOOP Instruction
• In real-address mode, CX is the default loop
counter for the LOOP instruction.
• On the other hand, the LOOPD instruction uses ECX
as the loop counter, and the LOOPW instruction uses
CX as the loop counter.
Exercise
INCLUDE Irvine32.inc
.code
main PROC
mov ax, 0
AX = 0
mov ecx, 5
ECX = 5
L1:
inc ax
AX = 1
AX = 2
loop Ll ECX = 4 ECX = 3
exit
main ENDP
END main
AX = 3
ECX = 2
AX = 4
ECX = 1
AX = 5
ECX = 0
Exercise
INCLUDE Irvine32.inc
.code
main PROC
mov ax, 0
AX = 0
mov ecx, 0
ECX = 0
L1:
inc ax
AX = 1
loop Ll ECX = FFFFFFFFh
exit
main ENDP
END main
If CX is the loop counter
(in real-address mode),
it repeats 65,536 times
AX = 2
ECX = FFFFFFFEh
The loop repeats
4,294,967,296
times
…
Exercise
INCLUDE Irvine32.inc
.code
main PROC
mov ax, 0
AX = 0
mov ecx, 5
ECX = 5
L1:
inc ax
AX = 1
AX = 2
inc ecx
loop Ll ECX = 5 ECX = 5
exit
main ENDP
END main
ECX is incremented
within the loop, so it
never reaches zero, and
the loop never stops
AX = 3
ECX = 5
…
…
LOOP Instruction
If you need to modify ECX
inside a loop, you can save it in
a variable at the beginning of
the loop and restore it just
before the LOOP instruction
INCLUDE Irvine32.inc
.data
count DWORD ?
.code
main PROC
mov ecx, 100
; set loop count
top:
mov count, ecx ; save the count
; modify ECX after saving its value
mov ecx, count ; restore loop count
loop top
exit
main ENDP
END main
Exercise (4)
• Write Assembly code having nested loops (a loop inside
another loop), where the outer loop executes 100 times, and the
inner loop executes 20 times.
• For example, in C++ language the above is implemented as
follows:
for (int i = 0; i < 100; i++)
{
// Inside the outer loop
for (int j = 0; j < 20; j++)
{
// Inside the inner loop
}
}
Exercise (4)
INCLUDE Irvine32.inc
.data
count DWORD ?
.code
main PROC
mov ecx, 100
; set outer loop count
L1:
mov count, ecx
; save outer loop count
mov ecx, 20
; set inner loop count
L2:
…
…
loop L2
; repeat the inner loop
mov ecx, count
; restore outer loop count
loop L1
; repeat the outer loop
exit
main ENDP
END main
Exercise
Copying a String
• Write Assembly code that uses a loop to copy a string,
represented as an array of bytes with a null terminator
value.
• Indexed addressing works well for this type of
operation because the same index register
references both strings.
• The target string must have enough available space
to receive the copied characters, including the null
byte at the end.
Exercise
INCLUDE Irvine32.inc
.data
source BYTE "This is the source string", 0
target BYTE SIZEOF source DUP (0)
.code
main PROC
mov esi, 0
; index register
mov ecx, SIZEOF source
; Initialize loop counter
L1:
mov al, source [esi]
; get a character from source
mov target[esi], al
; store it in the target
inc esi
; move to next character
loop L1
; repeat for entire string
exit
main ENDP
END main
JMP and LOOP Instructions
Control Transfer Types
Unconditional Transfer
Conditional Transfer
A new address is loaded into the
instruction pointer, causing
execution to continue at the new
address.
The JMP instruction does this.
The program branches if a
certain condition is true.
The CPU interprets true/false
conditions based on the contents
of the ECX and Flags registers.
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
LOOPZ / LOOPE Instructions
• They perform the following tasks:
ECX = ECX - 1
if ECX > 0 and ZF = 1, jump to
destination
• Otherwise, no jump occurs, and control passes to the
next instruction. .
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
LOOPNZ / LOOPNE Instructions
• They perform the following tasks:
ECX = ECX - 1
If ECX > 0 and ZF = 0, jump to
destination
• Otherwise, no jump occurs, and control passes to the
next instruction.
MASM directives
• MASM provides a set of program control
directives that look like those of high‐level
languages such as if‐else, while and repeat
statements. These directives are much easier
than using CMP and conditional jump
instructions. When assembling your program,
MASM translates statements use these
directives to the equivalent assembly code.
.IF Directive
.IF cond1
Statements
[.ELSEIF cond2
Statements]
[.ELSE
Statements]
.ENDIF
• Possible operators are ==, !=, >, >=, <, <=. Boolean expressions can
be accumulated using logical operators AND (&&), and OR (||).
• When translating .IF directives, MASM uses unsigned conditional
jumps by default however it uses signed conditional jumps if one
operand is defined as signed such as (SBYTE, SWORD…etc).
Example
INCLUDE Irvine32.inc
.data
uint1 dword 5
uint2 dword 10
int1 sdword -1
.code
main PROC
mov eax, 0
.IF eax > -1
mov eax, uint2
.endif
mov eax, 0
.IF eax > int1
mov eax, uint2
.endif
exit
main ENDP
END main
;Generated Code:
;CMP eax, -1
;JBE @C0001
;mov eax, uint2
;@C0001:
;Generated Code:
;CMP eax, int1
;JLE @C0002
;mov eax, uint2
;@C0002:
.REPEAT and .WHILE directives
.REPEAT
Statements
.UNTIL cond
.WHILE cond
Statements
.ENDW
Hands On
• Write an assembly program that copies an array to another
array. The array is entered by the user.
• Write an assembly program that calculates and prints the total
of an entered integer array.
• Write an assembly program that finds the min and max of an
entered integer array.
Hands On
• 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.)
• 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.
Assignment
• Assignment rules:
– Assignment should be delivered before the next
section
– Send your solution via mail
– Send .asm file (for each assignment)
– Write your full name & section in the mail
– Write in the subject (Assignment[#]_[Name]_
Section[#])
TA
Sections
Email
Amr ElSehemy
2,4
[email protected]
Ahd Abdel Razek
7, 12, 13
[email protected]
Loubna Ahmed
3,11
[email protected]
Yasser Daoud
1,10
Nora Youssef
8,9
Hanan Yousry
5, 6, 14
[email protected]
[email protected]
m
[email protected]
Assignment Problems
1- Write an assembly program that takes 10 integers and finds the two
largest values among input integers
2- 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.
Questions?
Thank you