0% found this document useful (0 votes)
10 views23 pages

CH05-Machine Language II

This document is a presentation on machine language and assembly instructions for an introductory computer systems course. It covers topics such as registers, operands, arithmetic operations, control flow, condition codes, and various types of loops. The content is based on a textbook and is intended for internal use only.

Uploaded by

zhwzhw1115
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views23 pages

CH05-Machine Language II

This document is a presentation on machine language and assembly instructions for an introductory computer systems course. It covers topics such as registers, operands, arithmetic operations, control flow, condition codes, and various types of loops. The content is based on a textbook and is intended for internal use only.

Uploaded by

zhwzhw1115
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 23

CH05

Machine Language II
COMP1411: Introduction to Computer Systems

Dr. Mingsong LYU ( 呂鳴松 )


Department of Computing,
The Hong Kong Polytechnic University
Spring 2025

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

movq %rdi, %rax


Þ result = x
cmpq %rsi, %rdi subq %rsi, %rax
Þ compute %rdi - %rsi (x-y) and set control flags Þ result = x – y
jle .L2
Þ (SF ^ OF) | ZF movq %rsi, %rax
If (x – y <= 0), either SF^OF is evaluated 1 or ZF is set to 1, the Þ result = y
condition for jle is evaluated TRUE, the jump to .L2 will be subq %rdi, %rax
taken, i.e., executing the “else” branch Þ result = y – x
Conditional branches by conditional moves
• cmovX: conditional move, move when the given condition is met
• gcc –m64 –Og –S –fif-conversion control.c
Variable Register
x %rdi
y %rsi
result %rax

Execute both branches


%rax : x – y
%rdx : y – x
Do the test later
cmpq %rsi, %rdi cmovle
Þ compute %rdi - %rsi (x-y) and set control flags
if (x <= y)
cmovle
Þ (SF ^ OF) | ZF %rdx  %rax
Þ %rax = y – x
If (x – y <= 0), either SF^OF is evaluated 1 or ZF is set to 1, the
condition for jle is evaluated TRUE, the jump to .L2 will be taken return %rax
Conditional branches by conditional moves
• Why using conditional moves?
• Sometimes, executing both branches will be faster then “test and jump”
• Because, modern processors use pipelines to execute a sequence of
instructions, and jump operation may cause the pipeline to stall
• Let’s revisit the problem in LEC06
Conditional branches by conditional moves
• Dangerous cases of conditional move
• CASE 1: Expensive computation

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

• CASE 2: risky computation


val
val == pp ?? *p
*p :
: 0;
0;

• This statement is to avoid referencing an address with value 0


• By conditional moves, the address is referenced anyway
• CASE 3: computation with side effects
val
val =
= x>
x> 0
0 ?
? x*=7
x*=7 :
: x+=3;
x+=3;

• Both values get computed


• Must ensure it is side-effect free

• Fortunately, the compiler will do the decision


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
“do-while” loop
• A general code structure for “do-while”loop

result = 0

temp = x & 0x1


result += temp
x >>= 1
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 %rax
“while” loop

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

for (init; test; update)


Body;

init; // while loop


goto L_TEST;
L_LOOP: init;
Body; if (test){
update; Body;
L_TEST: update;
if (test) }
goto
L_LOOP;
L_DONE:
Summary of machine language I & II
• The computer that assembly code works on
• General purpose registers + condition codes
• Data movement instructions
• Arithmetic and logical operation instructions
• Program flow control
• Condition codes
• Branches
• Loops
• Other topics uncovered due to limited course time
• Function calls
• Complex data structures, floating point instructions …
Thank You

You might also like