Assembly LanguageClass 6: Looping
CS 252 – Computer Organization & Assembly Language
Outline
› Implementing pretest loops
› Implementing posttest loops
› Implementing counter loops
› Basics
– The ecx register optimizations
› The jecxz instruction
› The loop instruction
Pretest Loops
› High level structure:
WHILE (Condition)
{{The body of
the loop}}
End WHILE
› Assembly level
implementation:
PretestCondition:
cmp … ;[!Condition]
jxx ExitLoop
{{The body of
the loop}}
jmp PretestCondition
ExitLoop:
Pretest Loops – Example
› Find the minimum integer N such that for a given
value Sum:
12 + 22 +…+ N2 ≥ Sum
Use the following design:
N = 1, PartSum = 1
WHILE (PartSum < Sum)
N++
PartSum += N2
END WHILE
Posttest Loops
› High level structure:
› Assembly level
implementation:
LoopAgain:
DO
{{The body of
the loop}}
WHILE (Condition)
{{The body of
the loop}}
cmp … ;[Condition]
jxx LoopAgain
Posttest Loops - Example
› Calculate the integer part of base 3 logarithm for
any positive 32-bit integer VarX. Use the following
design:
Logarithm = 0
DO
Logarithm++
WHILE (3Logarithm ≤ VarX)
RETURN (Logarithm-1)
Counter Loops
› High level structure:
› Assembly level implementation:
mov ecx, N
FOR i=N TO M STEP T
{{The body of
the loop}}
END FOR
PretestCondition:
cmp ecx, M
jxx ExitLoop ;js/jns…
{{The body of the loop}}
add ecx, T
;or sub
jmp PretestCondition
ExitLoop:
Counter Loops - Example
› Calculate the sum of the first N elements in
Fibonacci series: 1,1,2,3,5,8,13,... For any N > 2.
Use the following design:
Fib1 = 1, Fib2 = 1, Sum = 2
FOR i=3 TO N STEP 1
Fib3 = Fib1 + Fib2
Sum = Sum + Fib3
Fib1 = Fib2
Fib2 = Fib3
END FOR
The ecx Register Optimization
› The ecx register is optimized to be used as a
decrementing counter.
› There are direct assembly instructions (and
therefore machine instructions) to decrement and
test if the value in ecx.
The jecxz Instruction
› The syntax of the instruction is:
jecxz
StatementLabel
› If ecx = 0, the execution of the program is
transferred to the instruction labeled
StatementLabel.
› All properties of jxx instructions apply.
– The instruction does not modify the flags
The loop Instruction
› The syntax of the instruction is:
loop
StatementLabel
› Effect is equivalent to:
dec ecx
jecxz ExitLoop
jmp StatementLabel
ExitLoop:
;ecx = ecx -1
;exit if ecx=0
;otherwise loop
› The loop instruction does not modify the flags.
The loop Instruction – Example
› Calculate the sum of the first N elements in the
series:
3, (3-1)*2=4, (4-1)*2=6, (6-1)*2=10,…
Use the following design:
K = 3, Sum = 0
FOR i=1 TO N STEP 1
Sum += K
K = (K-1)*2
END FOR
Assembly Language Programming – Tip 6
› Ensure your loops always terminate. Issues that
may cause loops to run indefinitely:
– The termination condition can never occur.
– The code never tests for the termination condition.
– When the termination condition is met, the conditional branch
causes the loop to be performed again.
Modifying loop counter and control variables inside
the loop requires special caution.
Self-Practice Problems
1. Find the first element in Fibonacci series greater
than a positive 32-bit integer VarX.
Use the following design:
Fib1 = 1, Fib2 = 1, Fib3 = 2
WHILE (Fib3 MaxVal) THEN MaxVal = X
IF (X < MinVal) THEN MinVal = X
END WHILE
Self-Practice Problems (cont.)
3. Bit operations can be used to implement
arithmetic (specially in basic CPU’s). Write the
below code, in Assembly Language, to implement
addition using bit operations:
REM: Addends initially stored in A, and B
WHILE B 0
C = A xor B
D = (A and B) shl 1
A = C
B = D
END WHILE
Assembly Language
Class 4: Bit Operations
CS 252 – Computer Organization & Assembly Language
Outline
› Basics
– Logical operations
› Logical instructions:
– and, or, xor
– not
– test
› Shifting instructions: shl and shr instructions
Logical Operations
AND
True
False
OR
True
False
True
True
False
True
True
True
False
False
False
False
True
False
XOR
True
False
NOT
True
False
True
True
False
False
True
False
False
True
Logical Instructions: and, or, xor
› Logical instructions perform a bitwise logical
operation on the operands (0 is false, 1 is true):
and destination, source
or destination, source
xor destination, source
› Restrictions on the operands are similar to mov,
add,…
› CF and OF are cleared, SF and ZF are modified
based on the final value in destination.
Logical Instructions: not
› Performs a bitwise not on the operand:
not destination
› Does not affect the flags.
Logical Instructions: test
› To test the effect of and on flags without
modifying the destination:
test destination, source
› Rules and effects same as and, but:
– does not affect the destination.
Logical Instructions – Examples
Instruction
Operands (before)
Operands (after)
SF ZF CF OF
and al, ah
al: 0110 0010b
ah: 0011 0100b
al: 0010 0000b
ah: 0011 0100b
0 0 0 0
or bx, cx
bx:1001001101101001b
cx:0111010011010010b
bx:1111011111111011b
cx:0111010011010010b
1 0 0 0
xor al, 92h
al: 0101 0011b
92h=1001 0010b
al: 1100 0001b
1 0 0 0
not eax
eax: 07 31 00 75
eax: F8 CE FF 8A
test al, ah
al: 0110 0010b
ah: 0001 0100b
al: 0110 0010b
ah: 0001 0100b
0 1 0 0
Logical Instructions – Examples
› Using bit instructions design a mask that will clear
bit 3, reverse bit 4 and 5, and set bit 7 in any given
byte variable.
Logical Instructions – Examples
› Using bit instructions calculate VarX % 32 for
any given unsigned 32-bit integer VarX.
Logical Instructions – Examples
› What is the effect of the below code.
mov eax, 12345678h
mov ebx, 9ABCDEF0h
xor eax, ebx
xor ebx, eax
xor eax, ebx
Logical Instructions – Examples
› ASCII codes of the lowercase and uppercase of
any letter differ in bit 5 only (i.e. by clearing bit 5
of the lowercase you get the uppercase and vice
versa). So, using bit instructions toggle the case
of any given letter.
Shift Instructions: shl and shr
› Have the syntax:
shx destination, count
› Slide the bits in the destination by count
steps.
› shl slide to the left, shr slide to the right.
› Bits in the direction of sliding are thrown.
› Vacant bits at other end are filled with 0.
Shift Instructions: shl and shr
› destination can be a register or memory. Byte,
word, dword or quad size.
› source is either 1, direct byte value, or the
register cl.
› CF hold the last bit thrown, SF and ZF are modified
based on the final value in destination.
Shift Instructions: shl and shr - Example
› What does this code perform on the value in al:
mov ah, al
shl ah, 4
shr al, 4
or al, ah
Shift Instructions: shl and shr - Example
› How is the value in eax affected by the following:
–shl eax, 1
–shl eax, 4
–shr eax, 3
Assembly Language Programming – Tip 4
› Bit operations are the most primitive operations in
computers. Some CPU’s support only bit
operations. Most CPU’s experience very low cost
for bit operations.
Use bit operations to implement functions where
possible. This ensures your code is fast and
executable in simple low-cost processors.
Self-Practice Problems
1. The ASCII code of digits (0-9) is (0x30h-0x39h).
Thus every ASCII digit can be converted to its
numeric value by clearing the upper 4 bits of the
character.
write an assembly program that takes a character
digit and converts it to its value stored in a byte.
2. Write an instruction that sets the Zero flag if the
32-bit value in eax is even and clear the Zero flag
if eax is odd.
Self-Practice Problems
3. Write the code that calculates the parity (no of
1’s) of the 32-bit memory operand using the below
logic:
v = v - ((v >> 1) & 0x55555555);
v = (v & 0x33333333) + ((v >> 2) & 0x33333333);
c = ((v + (v >> 4) & 0xF0F0F0F) * 0x1010101) >> 24;
4. Given two bit-mapped sets named SetX and
SetY (of size DWORD), write a sequence of
instructions that generate a bit string in eax that
represents members in SetX that are not
members of SetY.
Assembly Language
Class 3: Basic Arithmetic
CS 252 – Computer Organization & Assembly Language
Outline
› Basics
– Hexadecimal arithmetic
– 2’s complement (negative numbers)
› The add and sub instructions
– eflags Register
› The inc and dec instructions
› The neg instruction
› The mul and imul instructions
› The div and idiv instructions
The add and sub Instructions
› The syntax of the instruction is:
add
destination, source
sub
destination, source
› The value originally stored in source is added
to/subtracted from destination.
destination = destination + source
destination = destination – source
› The value stored in source is not changed.
The add and sub Instructions
› Allowed parameters are the same as mov
instruction:
– Both destination and source should be same size.
– destination is either a register or memory location.
– source is either a register, a memory location or a direct value.
– Memory to memory operands are not supported.
The add and sub Instruction – eflags
› The add and sub instruction affect 4 flags: SF,
ZF, CF and OF.
› SF is set to the same value of the MSB of result.
– For signed addition, SF=1 means a negative result while SF=0
means positive result.
› ZF is set (to 1) if the result is 0; otherwise unset.
The add and sub Instruction – CF
› In addition, CF is set to the value carried out of
the MSB.
– For unsigned addition, CF=1 means insufficient space in
destination while CF=0 means sufficient space.
› In subtraction, CF is set to the value borrowed to
the MSB.
– For unsigned addition, CF=1 means negative result while CF=0
means positive result.
The add Instruction – OF
› OF is set (to 1) if carry in and out MSB are
different.
– For signed addition, OF=1 means insufficient space in
destination while OF=0 means sufficient space.
MSB 1 MSB 2 Carry in Carry out MSB Result
0
0
0
0
0
0
0
1
0
1
0
1
0
0
1
0
1
1
1
0
1
1
0
1
0
1
1
1
1
1
(+ve) + (+ve) = (–ve)
(–ve) + (–ve) = (+ve)
The sub Instruction – OF
› OF is set (to 1) if borrow from and to the MSB are
different.
– For signed subtraction, OF=1 indicates an error in the operation
due to used size.
MSB 1 MSB 2 Borrow from Borrow to MSB Result
0
0
0
0
0
0
0
1
1
1
0
1
0
1
1
0
1
1
1
0
1
1
0
0
0
1
1
1
1
1
(+ve) – (–ve) = (–ve)
The add Instruction – Examples
Instruction
Operands (before)
Operands (after)
SF ZF CF OF
add al, ah
al: 0110 0010b
ah: 0011 0100b
al: 1001 0110b
ah: 0011 0100b
1 0 0 1
add bl, cl
bl: 1001 0011b
cl: 0110 1101b
bl: 0000 0000b
cl: 0110 1101b
0 1 1 0
add al, 92h
al: 1101 0011b
92h=1001 0010b
al: 0110 0101b
0 0 1 1
add eax, ecx
eax: 00 00 00 75
ecx: 00 00 01 A2
eax: 00 00 02 17
ecx: 00 00 01 A2
0 0 0 0
add ax, cx
ax: 77 AC
cx: 4B 35
ax: C2 E1
cx: 4B 35
1 0 0 1
The sub Instruction – Examples
Instruction
Operands (before)
Operands (after)
SF ZF CF OF
sub al, ah
al: 1010 0010b
ah: 0111 0100b
al: 0010 1110b
ah: 0111 0100b
0 0 0 1
sub bl, cl
bl: 1100 0011b
cl: 1100 0011b
bl: 0000 0000b
cl: 1100 0011b
0 1 0 0
sub al, 92h
al: 0101 0011b
92h=1001 0010b
al: 1100 0001b
1 0 1 1
sub eax, ecx
eax: 00 00 01 75
ecx: 00 00 00 A2
eax: 00 00 00 D3
ecx: 00 00 00 A2
0 0 0 0
sub ax, cx
ax: 3A AC
cx: 50 3D
ax: EA 6F
cx: 50 3D
1 0 1 0
The inc and dec Instructions
› The syntax of the instruction is:
inc destination ; add destination,1
dec destination ; sub destination,1
› destination is either a register or memory
address.
› The instructions affect SF, ZF, and OF in the same
way their equivalent instructions do.
› The instructions do not affect CF.
The inc and dec Instructions – Examples
Instruction
Operands (before)
Operands (after)
SF
ZF
OF
inc cx
cx: 90 FF
cx: 91 00
1
0
0
inc ah
ah: 7F
ah: 80
1
0
1
inc ebx
ebx: FF FF FF FF
ebx: 00 00 00 00
0
1
0
inc word1
word1: 03 7E
word1: 03 7F
0
0
0
dec ax
ax: 83 90
ax: 83 8F
1
0
0
dec ch
ch: 80
ch: 7F
0
0
1
dec eax
eax: 00 00 00 01
eax: 00 00 00 00
0
1
0
dec word1
word1: 00 00
word1: FF FF
1
0
0
The neg Instruction
› The syntax of the instruction is:
neg destination
› The value originally in destination is replaced
with its 2’s complement (i.e. its negative).
› destination is either a register or memory
address.
› The instruction affects SF and ZF as all previous
instructions.
The neg Instruction – Examples
Instruction
Operands (before)
Operands (after)
SF
ZF
neg ax
ax: 3A 6F
ax: C5 91
1
0
neg eax
eax: 00 00 00 00
eax: 00 00 00 00
0
1
neg byte1
byte1: FF
byte1: 01
0
0
neg ebx
ebx: 12 34 56 78
ebx: ED CB A9 88
1
0
The mul and imul Instructions
› The syntax of these instructions is:
mul source
and
imul source
› mul performs unsigned integer multiplication,
imul performs signed integer multiplication.
› source is either a register or memory address
(no direct values).
› source is not modified unless it is part of the
destination registers.
The mul and imul Instructions
› Multipliers are source and same size
accumulator.
– The product is stored in the same size accumulator extended by
another same size register. (Why extended?)
Size of source
Implicit multiplier
Result low
Result high
Full result
BYTE (8 bits)
al
al
ah
ax (16 bits)
WORD (16 bits)
ax
ax
dx
dx:ax (32 bits)
DWORD (32 bits)
eax
eax
edx
edx:eax (64 bits)
The mul and imul Instructions
› The instruction sets both CF and OF if the upper
part of destination is significant. i.e.:
– mul: The upper part is not zero.
– imul: Any bit in the upper part of the result is different than the
MSB of the lower part.
The mul and imul Instructions – Examples
Instruction
mul bl
Operands (before)
bl: FF
al: 05
Operands (after)
ax: 04 FB
CF OF
1 1
mul ebx
ebx: 00 00 00 02
eax: 00 00 00 05
eax: 00 00 00 0A
edx: 00 00 00 00
0
0
mul value1
value1: 08 F2
ax: 00 10
ax: 8F 20
dx: 00 00
0
0
imul bl
bl: FF
al: 05
ax: FF FB
0
0
imul bx
bx: 00 02
ax: 00 05
ax: 00 0A
dx: 00 00
0
0
imul value1
value1: 08 F2
ax: 00 10
ax: 8F 20
dx: 00 00
1
1
The imul Instruction – 2 and 3 Operands
› 2-operands version has same format as add.
– Syntax: imul destination, source
– destination should be a register
– Byte operands are not allowed
› 3-operands version has the syntax:
imul register, source, directvalue
– source is either a register or memory address
– Byte operands not allowed
› In both versions, CF and OF are set if the product does
not fit in the register destination.
The div and idiv Instructions
› The syntax of the instructions is:
div
source
and idiv source
› div performs unsigned integer division;
idiv performs signed integer division.
› source is either a register or memory (but not
direct value).
The div and idiv Instructions
› With: dividend = divisor * quotient + remainder
• E.g. 22 = 6 * 3 + 4
• source is the divisor.
Size of source
BYTE (8 bits)
Implicit dividend
ax (16 bits)
Quotient
al
Remainder
ah
WORD (16 bits)
dx:ax (32 bits)
ax
dx
DWORD (32 bits)
edx:eax (64 bits)
eax
edx
› In signed division, the sign of the remainder is the
same as dividend:
• (-22) = 6 * (-3) + (-4) ✓
• (-22) = 6 * (-4) + (2)
(-22) = (-6) * 3 + (-4)
(-22) = (-6) * 4 + (2)
✓
The div and idiv Instructions
› Arithmetic flags values are undefined after
executing div or idiv.
› An exception is raised if an error occurs in
division:
– Division by zero (i.e. source = 0)
– Division overflow (i.e. too large quotient)
The div and idiv Instructions – Examples
Instruction
div bl
Operands (before)
ax: 90 FF
bl: F4
Operands (after)
al: 98
ah: 1F
Operation
37119/244 =
152 r 31
idiv bl
ax: 90 FF
bl: F4
Overflow
exception
(-28417)/(-12)
= (-2365) r(-1)
div dword1
dword1: 00 00 00 0D
eax: 00 00 00 64
edx: 00 00 00 00
eax: 00 00 00 07
edx: 00 00 00 09
100/13 = 7 r 9
idiv dword1 dword1: 00 00 00 0D
eax: FF FF FF 9C
edx: FF FF FF FF
eax: FF FF FF F9
edx: FF FF FF F7
(-100)/13 =
(-7) r (-9)
The cbw, cwd, and cdq Instructions
› These instructions sign-extends the values in
lower half of implicit dividend to the upper half.
– That is useful before executing idiv
› The instructions are no-operand-instructions.
Assembly Language Programming – Tip 3
› The cost of all instructions is not equal.
› For example, the 2 instructions:
add eax, eax
and
imul eax, 2
have the same effect but the latter is much more
costly (computationally). i.e. It requires more
processor cycles.
As a programmer, whether in low or high level, you should
pick the most inexpensive instructions that complete the
required operation.
Self-Practice Problems
1. Using mov and add instructions only calculate
the perimeter of a rectangle given its 2 sides.
Perimeter = 2(Width + Length)
2. To convert temperature from Celsius to
Fahrenheit we use the formula:
Fahrenheit =
9
Celsius
+
32
5
Given the value of temperature in Celsius write
an assembly code to calculate Fahrenheit.
Self-Practice Problems (cont.)
3. The surface area of a box is calculated as:
Area = 2(length*height + height*width + width*length)
Write an assembly program to calculate Area
(given the 3 dimensions).
4. Given P 𝑥 = 𝑥 4 + 𝑥 3 + 𝑥 2 + 𝑥. Write the
assembly code to calculate P 𝑥 , if 𝑥 is stored in
eax.
5. Calculate 15𝑥 using add and sub instructions.
Use 16 bit integers.
Assembly Language
Class 2: Copying Data
CS 252 – Computer Organization & Assembly Language
Outline
› Simple Assembly Program
› Reserving memory
› The mov instruction
› The movzx and movsx instructions
› The xchg instruction
Simple Assembly Language Program
.DATA
.CODE
The program
contains 2 main
sections:
DATA section for
reserving memory
locations for
variables and
CODE section for
instructions.
Simple Assembly Language Program
.Assembler directives
Commands that are recognized and
acted upon by the assembler
• Not part of the Intel instruction
set
• Used to declare code, data
areas, select memory model,
declare procedures, etc.
• not case sensitive
Different assemblers have different
directives
NASM not the same as MASM, for
example
IRVINE, KIP R. ASSEMBLY LANGUAGE FOR X86 PROCESSORS 7/E, 2015.
Simple Assembly Language Program
; Comments
Comments are
solely for human
use; it has no
effect on the
assembly process
or on the
generated
program
Simple Assembly Language Program
Instructions
The statements
that are actually
executed by the
program
Label:
Opn Para1, Para2
Assembly Language Programming – Tip 1
Assembly language is NOT case sensitive (except
for user defined labels).
All the above code snippets are equivalent.
However, using a consistent and elegant coding style
makes your programs readable, understandable and
maintainable.
Hexadecimal Numbers
› To represent binary numbers in more readable
form, pack it as hexadecimal (base 16) numbers.
0000
0100
1000
1100
0
4
8
C
0001
0101
1001
1101
1
5
9
D
0010
0110
1010
1110
2
6
A
E
0011
0111
1011
1111
3
7
B
F
› Every byte is represented by 2 hexadecimals:
1100 0110 b = 0xC6 h
1111 1001 b = 0xF9 h
32, 16 and 8-Bit Registers
› In 32-bit machines:
– 16-bit registers are the lower 2 bytes of 32-bit registers.
– The 16-bit registers are divided into 2 8-bit registers.
MSB – Most Significant Bit
Least Significant Bit – LSB
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9
8
7
AH
6
5
4
3
2
1
AL
AX
EAX
› The same structure applies to EBX, ECX and EDX.
0
32, 16 and 8-Bit Registers
› If a hexadecimal value of 0x12345678 is stored in
EAX, then:
AX is 0x5678,
AL is 0x78, and
AH is 0x56.
1
2
3
4
5
6
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9
7
8
7
AH
5
4
3
AL
AX
EAX
6
8
2
1
0
32, 16 and 8-Bit Registers
› If AH is modified to 0xAB then
EAX becomes 0x1234AB78
AX becomes 0xAB78,
AL remains 0x78.
1
2
3
A
4
B
31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9
7
8
7
AH
5
4
3
AL
AX
EAX
6
8
2
1
0
Memory Labels – .DATA Section
› Memory is reserved for variables in the .DATA
section.
.DATA
number1
number2
number3
DWORD 112
DWORD ?
WORD 1998
Memory Variables – High-level languages
› High-level languages declare variable. This
includes: size and type of data.
int
number1; // 4 bytes signed integer
uint
number2; // 4 bytes unsigned integer
long
number3; // 8 bytes unsigned integer
float number4; // 4 bytes real number
Double number5; // 8 bytes real number
Memory Labels – Assembly language
› Assembly language reserves memory of specific
size.
› It is the programmer responsibility to manage the
type of content.
.DATA
number1
number2
number3
letter1
letter2
DWORD -1000
DWORD 872552
DWORD 1.6667
BYTE ‘d’
BYTE 127
Memory Labels – Memory Sizes
› The size of memory reserved is specified using 4
pseudo-instructions.
.DATA
number1
number2
number3
number4
BYTE 100 ; 1 byte, 256 distinct values
WORD 1200 ; 2 bytes, 65,536 values
DWORD 14 ; 4 bytes, 4,294,967,296 values
QWORD 120 ; 8 bytes, 18×10^18 values
Memory Labels – Initialization
› The reserved memory can be initialized in the
same pseudo-instructions.
.DATA
number1
number2
number3
letter1
letter2
BYTE 100
BYTE -1
BYTE 255
BYTE ‘B’
BYTE ?
; 1 byte, 0x64
; 1 byte (-127-128), 0xFF
; 1 byte (0-255), 0xFF
; 1 byte (not initialized)
› Note that number2 and number3 are initialized
equally.
Memory Labels – Initialization
› Similarly, the 4 bytes memory reserved for
number4, number5 and number6 are all
initialized to the same values.
number4
number5
number6
DWORD 4294967295
DWORD -1
DWORD 0FFFFFFFFh
; 4 bytes, 0xFFFFFFFF
; 4 bytes, 0xFFFFFFFF
; 4 bytes, 0xFFFFFFFF
The mov Instruction
› mov is the main instruction used to copy data.
› The syntax of the statement is:
mov
destination, source
› The value originally stored in source is copied to
destination.
› The value originally stored in destination is
overwritten (lost).
The mov Instruction – Parameters
› mov
destination, source
› destination is either a register or memory
location.
› source can be a register, a memory location, or
a direct value.
mov eax, number1 ; memory to register
mov number1, eax ; register to memory
mov bx, cx ; register to register
mov al, 127 ; direct value to register
The mov Instruction – Illegal parameters
› Memory to memory mov is not supported.
› To copy data from memory to memory, a register
intermediary should be used.
; number2 := number1
mov eax, number1 ; eax
:= number1
mov number2, eax ; number2 := eax
The mov Instruction – Illegal parameters
› destination and source should be of same
size.
› To copy data from small size source to large size
destination, fill higher bytes of destination with
suitable value and copy source to lower bytes.
; word1 := byte1
mov ah, 00h
; higher byte to 0
mov al, byte1 ; lower byte to byte1
mov word1, ax ; word1 = ax
movzx Instruction
› These instructions allow copying data from small
size source to larger size destination.
› movzx copies the source in lower bytes then
zero-extends the destination.
– This is useful for unsigned integers.
0
10001111
Source
00000000
10001111
Destination
mov bl,10001111b
movzx ax,bl
; zero-extension
IRVINE, KIP R. ASSEMBLY LANGUAGE FOR X86 PROCESSORS 7/E, 2015.
movsx Instruction
› movsx copies the source in lower bytes then
sign-extends the destination (i.e. extends the most
significant bit).
– This is useful for signed integers.
11111111
10001111
Source
10001111
Destination
mov bl,10001111b
movsx ax,bl
; sign extension
IRVINE, KIP R. ASSEMBLY LANGUAGE FOR X86 PROCESSORS 7/E, 2015.
The movzx and movsx Instructions
mov bx, 0A69Bh
movzx eax, bx
movzx edx, bl
movzx cx, bl
movsx eax,bx
movsx edx,bl
movsx cx,bl
mov bx, 0364Bh
movsx eax,bx
movsx edx,bl
movsx cx,bl
; EAX = 0000A69Bh
; EDX = 0000009Bh
; CX = 009Bh
; EAX = FFFFA69Bh
; EDX = FFFFFF9Bh
; CX = FF9Bh
; EAX = 0000364Bh
; EDX = 0000004Bh
; CX = 004Bh
The xchg Instruction
› This instruction exchanges the contents of its
operands.
› xchg has 2 variants:
; Exchange register with register
xchg eax, ebx
xchg dx, bx
xchg al, ah
; Exchange register with memory
xchg eax, dwordnum1
xchg bytenum2, bh
The xchg Instruction
› Illegal xchg is the same as illegal mov.
› Exchange memory with memory is not supported.
› Exchanged operands should be the same size.
Assembly Language Programming – Tip 2
› Register operations are faster than memory
operations.
› Operations on the accumulator (register A)
generate more compact machine code.
As a programmer, you should plan to keep frequently
accessed data in registers rather than memory. Moreover,
use the accumulator for the most actively accessed data.
Your turn. . .
Write a program that rearranges the values of three doubleword
values in the following array as: 3, 1, 2.
.data
arrayD DWORD 1,2,3
• Step1: copy the first value into EAX and exchange it with the value
in the second position.
mov eax,arrayD
xchg eax,[arrayD+4]
• Step 2: Exchange EAX with the third array value and copy the
value in EAX to the first array position.
xchg eax,[arrayD+8]
mov arrayD,eax
IRVINE, KIP R. ASSEMBLY LANGUAGE FOR X86 PROCESSORS 7/E, 2015.
The Listing File
› One of the output files created by the assembler is
the listing file.
› The file lists the machine code associated with
each assembly instruction.
› This may help to understand (and appreciate)
assembly language better.
Self-Practice Problems
1. Using mov instruction(s), write an assembly
program that swaps the higher and lower bytes
of a memory word value.
2. Replace the illegal instruction below with a legal
code fragment.
.DATA
number1 WORD 1234h
number2 WORD 5678h
.CODE
; ILLEGAL: xchg number1, number2
Self-Practice Problems (cont.)
3. Re-write the movzx instruction below using mov
instruction(s) only:
movzx eax, Byte1 ; Byte1 has size 1
4. Write 2 code fragments that copy the least
significant byte from number1 and number2 to
number3:
.DATA
number1 DWORD 12345678h
number2 WORD 9ABCh
number3 BYTE ?
1501252: Computer Org. And Assembly Lang.
Spring 2022-2023
Assignment#1
Due Date: February 26th, 2023
Name:
ID:
Date:
Section:
Submit your assignment on the blackboard link
Please follow the following rules to submit the assignment:
•
•
The name of the answer file should contain your ID.
In case two assignments are found to be similar, both students will get ZERO.
Marking Scheme:
Theory
Score
Weight
Part 1
2
Part 2
2
Part 3
3
Part 4
3
Total
10
Part 1: Problem 1
Given three signed 32-bit integers A, B and C, write an assembly program to compute:
A%B – B × C. Store the final result in eax.
Sample IO data:
VarA
DWORD
210
VarB
DWORD
90
VarC
DWORD
352
Final value in eax -31650
VarA
DWORD
-200
VarB
DWORD
33
VarC
DWORD
77
Final value in eax -2543
VarA
DWORD
3966
VarB
DWORD
-69
VarC
DWORD
412
Final value in eax 28461
1/3
Part 2: Problem 2
Implement the following equation in assembly language, using 16-bit signed integers:
𝐷𝐷 = −((𝐴𝐴 − 𝐵𝐵) + (𝐶𝐶 + 1)).
Part 3: Problem 3
Starting with the windows32 framework, write a complete program that will input values for a, b and
c and display the value of the expression
𝑎𝑎 + 𝑏𝑏 ∗ 𝑐𝑐
2 ∗ 𝑏𝑏
Do not round. Input and output must be consistent with the samples shown at the bottom of
this page.
2/3
Part 4: Problem 4
Starting with the windows32 framework, write a complete assembly program to prompt for four
grades. Suppose that the last grade is a final exam grade that counts twice as much as the other
three. Display the sum (adding the last grade twice) and the average (sum/5).
3/3
Assembly Language for x86 Processors
7th Edition , Global Edition
Kip Irvine
Chapter 1: Basic Concepts
Slides prepared by the author
Revision date: 1/15/2014
(c) Pearson Education, 2015. All rights reserved. You may modify and copy this slide show for your personal use, or for
use in the classroom, as long as this copyright statement, the author’s name, and the title are not changed.
Chapter Overview
•
•
•
•
Welcome to Assembly Language
Virtual Machine Concept
Data Representation
Boolean Operations
Irvine, Kip R. Assembly Language for Intel-Based Computers 7/e, 2015.
2
Welcome to Assembly Language
• Some Good Questions to Ask
• Assembly Language Applications
Irvine, Kip R. Assembly Language for Intel-Based Computers 7/e, 2015.
3
Questions to Ask
•
•
•
•
•
•
•
Why am I learning Assembly Language?
What background should I have?
What is an assembler?
What hardware/software do I need?
What types of programs will I create?
What do I get with this book?
What will I learn?
Irvine, Kip R. Assembly Language for Intel-Based Computers 7/e, 2015.
4
Welcome to Assembly Language (cont)
• How does assembly language (AL) relate to machine
language?
• How do C++ and Java relate to AL?
• Is AL portable?
• Why learn AL?
Irvine, Kip R. Assembly Language for Intel-Based Computers 7/e, 2015.
5
Assembly Language Applications
• Some representative types of applications:
• Business application for single platform
• Hardware device driver
• Business application for multiple platforms
• Embedded systems & computer games
(see next panel)
Irvine, Kip R. Assembly Language for Intel-Based Computers 7/e, 2015.
6
Comparing ASM to High-Level Languages
Irvine, Kip R. Assembly Language for Intel-Based Computers 7/e, 2015.
7
What’s Next
•
•
•
•
Welcome to Assembly Language
Virtual Machine Concept
Data Representation
Boolean Operations
Irvine, Kip R. Assembly Language for Intel-Based Computers 7/e, 2015.
8
Virtual Machine Concept
• Virtual Machines
• Specific Machine Levels
Irvine, Kip R. Assembly Language for Intel-Based Computers 7/e, 2015.
9
Virtual Machines
• Programming Language analogy:
• Each computer has a native machine language (language
L0) that runs directly on its hardware
• A more human-friendly language is usually constructed
above machine language, called Language L1
• Programs written in L1 can run two different ways:
• Interpretation – L0 program interprets and executes L1
instructions one by one
• Translation – L1 program is completely translated into an L0
program, which then runs on the computer hardware
Irvine, Kip R. Assembly Language for Intel-Based Computers 7/e, 2015.
10
Translating Languages
English: Display the sum of A times B plus C.
C++: cout 7, the value is
negative. Examples: 8A, C5, A2, 9D
Irvine, Kip R. Assembly Language for Intel-Based Computers 7/e, 2015.
32
Forming the Two’s Complement
• Negative numbers are stored in two’s complement
notation
• Represents the additive Inverse
Note that 00000001 + 11111111 = 00000000
Irvine, Kip R. Assembly Language for Intel-Based Computers 7/e, 2015.
33
Binary Subtraction
• When subtracting A – B, convert B to its two’s
complement
• Add A to (–B)
00001100
– 00000011
00001100
11111101
00001001
Practice: Subtract 0101 from 1001.
Irvine, Kip R. Assembly Language for Intel-Based Computers 7/e, 2015.
34
Learn How To Do the Following:
•
•
•
•
•
Form the two’s complement of a hexadecimal integer
Convert signed binary to decimal
Convert signed decimal to binary
Convert signed decimal to hexadecimal
Convert signed hexadecimal to decimal
Irvine, Kip R. Assembly Language for Intel-Based Computers 7/e, 2015.
35
Ranges of Signed Integers
The highest bit is reserved for the sign. This limits the range:
Practice: What is the largest positive value that may be stored in 20 bits?
Irvine, Kip R. Assembly Language for Intel-Based Computers 7/e, 2015.
36
Character Storage
• Character sets
• Standard ASCII (0 – 127)
• Extended ASCII (0 – 255)
• ANSI (0 – 255)
• Unicode (0 – 65,535)
• Null-terminated String
• Array of characters followed by a null byte
• Using the ASCII table
• back inside cover of book
Irvine, Kip R. Assembly Language for Intel-Based Computers 7/e, 2015.
37
Numeric Data Representation
• pure binary
• can be calculated directly
• ASCII binary
• string of digits: “01010101”
• ASCII decimal
• string of digits: “65”
• ASCII hexadecimal
• string of digits: “9C”
next: Boolean Operations
Irvine, Kip R. Assembly Language for Intel-Based Computers 7/e, 2015.
38
What’s Next
•
•
•
•
Welcome to Assembly Language
Virtual Machine Concept
Data Representation
Boolean Operations
Irvine, Kip R. Assembly Language for Intel-Based Computers 7/e, 2015.
39
Boolean Operations
•
•
•
•
•
NOT
AND
OR
Operator Precedence
Truth Tables
Irvine, Kip R. Assembly Language for Intel-Based Computers 7/e, 2015.
40
Boolean Algebra
• Based on symbolic logic, designed by George Boole
• Boolean expressions created from:
• NOT, AND, OR
Irvine, Kip R. Assembly Language for Intel-Based Computers 7/e, 2015.
41
NOT
• Inverts (reverses) a boolean value
• Truth table for Boolean NOT operator:
Digital gate diagram for NOT:
NOT
Irvine, Kip R. Assembly Language for Intel-Based Computers 7/e, 2015.
42
AND
• Truth table for Boolean AND operator:
Digital gate diagram for AND:
AND
Irvine, Kip R. Assembly Language for Intel-Based Computers 7/e, 2015.
43
OR
• Truth table for Boolean OR operator:
Digital gate diagram for OR:
OR
Irvine, Kip R. Assembly Language for Intel-Based Computers 7/e, 2015.
44
Operator Precedence
• Examples showing the order of operations:
Irvine, Kip R. Assembly Language for Intel-Based Computers 7/e, 2015.
45
Truth Tables (1 of 3)
• A Boolean function has one or more Boolean inputs,
and returns a single Boolean output.
• A truth table shows all the inputs and outputs of a
Boolean function
Example: X Y
Irvine, Kip R. Assembly Language for Intel-Based Computers 7/e, 2015.
46
Truth Tables (2 of 3)
• Example: X Y
Irvine, Kip R. Assembly Language for Intel-Based Computers 7/e, 2015.
47
Truth Tables (3 of 3)
• Example: (Y S) (X S)
S
X
mux
Z
Y
Two-input multiplexer
Irvine, Kip R. Assembly Language for Intel-Based Computers 7/e, 2015.
48
Summary
• Assembly language helps you learn how software is
constructed at the lowest levels
• Assembly language has a one-to-one relationship
with machine language
• Each layer in a computer’s architecture is an
abstraction of a machine
• layers can be hardware or software
• Boolean expressions are essential to the design of
computer hardware and software
Irvine, Kip R. Assembly Language for Intel-Based Computers 7/e, 2015.
49
54 68 65 20 45 6E 64
What do these numbers represent?
Irvine, Kip R. Assembly Language for Intel-Based Computers 7/e, 2015.
50
Question 1:
Implement an assembly program that prompts users to input three 32-bit signed operands. The program
then outputs the result as shown in the following C++ expression.
var4 = (var1 * 5) / (var2 – var3);
; Example assembly language program — adds two numbers
; Author: R. Detmer
; Date: 1/2008
.586
.MODEL FLAT
INCLUDE io.h
; header file for input/output
.STACK 4096
.DATA
var1 DWORD ?
var2 DWORD ?
var3 DWORD ?
prompt1 BYTE “Enter var1”, 0
prompt2 BYTE “Enter var2”, 0
prompt3 BYTE “Enter var3”, 0
string BYTE 40 DUP (?)
resultLbl BYTE “Var4 is”, 0
var4 DWORD 11 DUP (?), 0
.CODE
_MainProc PROC
input prompt1, string, 40
; read ASCII characters
atod string
; convert to integer
mov var1, eax ; store in memory
input prompt2, string, 40
atod string
mov var2, eax
; repeat for second number
input prompt3, string, 40
atod string
mov var3, eax
; repeat for third number
; Write your code here
mov eax,var1
mov edx, 5
mul edx
mov ebx,var2
mov ecx, var3
sub ebx,ecx
div ebx
1/3
mov var4,edx
dtoa var4, eax
; convert to ASCII characters
output resultLbl, var4
; output label and sum
mov eax, 0 ; exit with return code 0
ret
_MainProc ENDP
END
; end of source code
Question 2:
Using mov and add instructions, only calculate the perimeter of a rectangle given its two sides.
Perimeter = 2(Width + Length).
; lab3
.386
.model flat,stdcall
.stack 4096
ExitProcess proto,dwExitCode:dword
.data
; declare variables here
w DWORD 4
l DWORD 5
p DWORD ?
.code
main proc
; write your code here
mov eax,w
add eax,l
add eax,w
add eax, l
mov p, eax
invoke ExitProcess,0
main endp
end main
2/3
3/3