CH05-Machine Language II
CH05-Machine Language II
Machine Language II
COMP1411: Introduction to Computer Systems
Acknowledgement: These slides are based on the textbook (Computer Systems: A Programmer’s Perspective) and its slides.
These slides are only intended to use internally. Do not publish it anywhere without permission.
Outline
• What are assembly/machine instructions
• Registers and operands
• Operations
• Moving data between memory and CPU
• Arithmetic and logical operations
• Control
• Condition codes --- special registers
• Conditional branches
• Loops
An example of control in a C program
• A program not only performs
• z = x + y;
• a = b & c;
• ......
• It also performs different actions according to some condition
x = 5;
y = 7;
if (x > y){ // the program needs to know T/F
x = x + y;
}
else{
y = x + y;
}
General purpose registers
• General purpose registers used for data movement and
arithmetic/logical computation
%rax %r8
%rbx %r9
%rcx %r10
%rdx %r11
%rsi %r12
%rdi %r13
%rsp %r14
%rbp %r15
Condition codes
• Condition codes
• Special single-bit registers to record the states/properties of an instruction
• Not visible to programmers, implicitly set by different types of instructions
• Explicitly read by special instructions
CF Carry Flag
ZF Zero Flag
SF Sign Flag
OF Overflow Flag
Case 1: set by arithmetic operations
CF ZF SF OF
addq Src,Dest → t = a + b
CPUs are “cold-blooded”: they only deal with binary vectors,
they are totally ignorant of data types, e.g., signed or unsigned
CODE Definition (of set to 1) Typical Usage
If carry in from the most To detect overflow cases if data items are
CF
significant bit understood as unsigned
Indicating zero, no matter the data items are
ZF if the result t is zero understood as signed or unsigned
a copy of the most significant Indicating negative result if data items are
SF
bit of the dest understood as signed
Look at the most significant bit:
To detect overflow cases if data items are
OF • 0 (src) and 0 (dest) 1 (dest)
understood as signed
• 1 (src) and 1 (dest) 0 (dest)
Case 1: set by arithmetic operations
• Examples to explain the definitions
1 1 0 0 0 1 1 1
+ 0 1 0 0 + 0 0 1 0
1 0 0 0 0 1 0 0 1
CF = 1 SF = 0 SF = 1 OF = 0 CF = 0 SF = 1 SF = 0 OF = 1
Case 1: set by arithmetic operations
• Usages of the conditional codes
• SF: only meaningful if data items are understood as signed integers, we
use this conditional code to know we have a negative number resulted
• ZF: no matter signed or unsigned, “ZF = 1” means the result is zero
• CF: to detect overflow cases if data items are understood as unsigned
• OF: to detect overflow cases if data items are understood as signed
1 1 0 0 0 1 1 1 1 0 0 1
+ 0 1 0 0 + 0 0 1 0 + 1 1 1 0
1 0 0 0 0 1 0 0 1 1 0 1 1 1
Unsigned: Signed: Signed:
12 + 4 = 16 7+2=9 -7 + (-2) = -9
Overflow Overflow Overflow
CF = 1 OF = 1 OF = 1
The CPU said to higher level program: I have no idea whether you are representing signed or
unsigned numbers, I simply set the conditional codes. If you are assuming signed or unsigned,
please read the conditional codes by yourself, and understand those codes by yourself.
Case 2: set by compare/set instructions
CF ZF SF OF
cmp* b,a
• * = q,l,w,b
• a and b are specified by registers
• cmpl b,a like to do arithmetic computation a-b
• But the values of a and b are not changed
test* b,a
• * = q,l,w,b
• a and b are specified by registers
• testl b,a like computing a&b (bit wise &)
• But the values of a and b are not changed
Reading condition codes
• SetX Instructions
• setX %register
• Read one condition code or a combination of multiple condition codes
• and set the corresponding value (1 or 0) to the given single-byte register
SetX Condition Description
sete ZF Equal / Zero
setne ~ZF Not Equal / Not Zero
sets SF Negative
setns ~SF Nonnegative
setg ~(SF^OF)&~ZF Greater (Signed)
Greater or Equal
setge ~(SF^OF)
(Signed)
setl (SF^OF) Less (Signed)
setle (SF^OF)|ZF Less or Equal (Signed)
seta ~CF&~ZF Above (unsigned)
setb CF Below (unsigned)
Reading condition codes - example
• Understanding setg
• ~(SF^OF) & ~ZF
• Used to indicate “greater” relationship for signed numbers
• cmpl y,x (like computing x–y)
• setg %al (put the evaluation result to the register %al)
• Assume x and y are signed integers
• Case 1: no overflow
• Case 1.1: x==y, ZF = 1, ~ZF = 0 %al = 0
• Case 1.2: x < y, ZF = 0, ~ZF = 1; SF = 1, OF = 0, ~(SF^OF) = 0 %al = 0
• Case 1.3: x > y, ZF = 0, ~ZF = 1; SF = 0, OF = 0, ~(SF^OF) = 1 %al = 1 (x is greater
than y)
• Case 2: overflow
• Case 2.1: x<0,y>0, negative overflow, SF = 0, OF = 1, ZF = 0 %al = 0
• Case 2.2: x>0,y<0, positive overflow, SF = 1, OF = 1, ZF = 0 %al = 1 (x is greater
than y)
Outline
• What are assembly/machine instructions
• Registers and operands
• Operations
• Moving data between memory and CPU
• Arithmetic and logical operations
• Control
• Condition codes --- special registers
• Conditional branches
• Loops
Jump instructions – change the control flow
• By default, a CPU executes a program from the first instruction to the last
instruction sequentially --- this does not allow branching semantics
• jX instructions
• Change the instruction sequence by jumping to a target instruction either
specified by absolution address or by a value in some register or memory
jX Condition Description cmp a, b
jmp 1 Unconditional ---
je ZF Equal / Zero b == a
jne ~ZF Not Equal / Not Zero b != a
js SF Negative b<a
jns ~SF Nonnegative b >= a
jg ~(SF^OF)&~ZF Greater (Signed) b>a
jge ~(SF^OF) Greater or Equal b >= a
(Signed)
jl (SF^OF) Less (Signed) b<a
jle (SF^OF)|ZF Less or Equal (Signed) b <= a
ja ~CF&~ZF Above (unsigned) b>a
jb CF Below (unsigned) b<a
Implementing conditional branches with jX
• gcc –m64 –Og –S control.c
Variable Register
x %rdi
y %rsi
result %rax
val
val =
= Test(x)
Test(x) ?
? Hard1(x)
Hard1(x) :
: Hard2(x);
Hard2(x);
• Both values get computed with complex logic that takes a long time
• Executing both branches will be not worthwhile
result = 0
result = 0
while(x)
if after x >>= 1, x == 0, the
ZF flag will be set to 1, and jne
will be evaluated FALSE, the
Variable Register
program will not loop back
x %rdi
result %eax
“for” loop
• “for” loop can be transformed into “do-while”loop or “while” loop