CH04-Machine Language I (1)
CH04-Machine Language I (1)
Machine Language I
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
• Conditional branches
• Loops
• Switch statements
First impression of assembly/machine code
printf.o
Pre-
hello.c hello.i Compiler hello.s Assembler hello.o Linker hello
processor
(cc1) (as) (ld)
(cpp)
Source Modified Assembly Relocatable Executable
program source program object object
(text) program (text) programs program
(text) (binary) (binary)
03 45 08 • Machine code
• 3-byte instruction (0x 034508)
Program execution
• A program is a set of machine instructions
• The execution of a program is the execution of a sequence of its
instructions which operates on input data and produced output
data
• A sequence of instructions cookbook
• Input data vegetables, meat, ingredients
• Output data the dish
CPU and main memory
• The main memory
• A program is first loaded into the main memory, so that it can be executed
• A program includes its instructions and the data the program needs to work on
• Byte addressable array, stores both instructions and data
• Accessing the main memory needs to know the address
• The CPU
• Arithmetic & Logic Unit (ALU): the device to do arithmetic computation
• Registers: temporarily store data inside the CPU
• PC: a special register stores the address of the next instruction to execute
0x0000
Register
Register ZF SF OF
file
file
ADDRESS Inst 1 0x0008
PC
Inst 2 0x0010
Inst 3 0x0018
A
A DATA
Data 1 0x0020
L
U Data 2 0x0028
B OF
ZF INSTRUCTIONS Data 3 0x0030
CF
0x0038
Main memory
Program execution – an example
Instruction 1:
Read a data item
from the main 300, 301: the addresses
memory of the instructions in the
main memory
• Assembly code
• addl: Add two 4-byte integers
addl (%ebp),%eax • Operands:
x: Register %eax
y: Memory M[%ebp]
Data types
• The size of a data item
• “word” in x86-64 ISA, a specific unit of data size
• 1 word = 2 bytes
• Double words: 4 bytes
• Quad words: 8 bytes
C declaration Intel data type Ass. inst. suffix Size (bytes)
char Byte b 1
short Word w 2
int Double word l 4
long Quad word q 8
char * Quad word q 8
float Single precision s 4
double Double precision l 8
X86-64 registers
• Registers are small-sized memory storage inside the CPU core,
used to store data (operated by the instructions)
0x0000
Register
Register ZF SF OF
file
file
ADDRESS Inst 1 0x0008
PC
Inst 2 0x0010
Inst 3 0x0018
A
A DATA
Data 1 0x0020
L
U Data 2 0x0028
B OF
ZF INSTRUCTIONS Data 3 0x0030
CF
0x0038
63 31 15 7 0
Register Value
swap: %rdi xp
movq (%rdi), %rax # t0 = *xp
%rsi yp
movq (%rsi), %rdx # t1 = *yp
movq %rdx, (%rdi) # *xp = t1 %rax t0
movq %rax, (%rsi) # *yp = t0 %rdx t1
ret
An example of data moving
Memory
Registers Address
123 0x120
%rdi 0x120
0x118
%rsi 0x100
0x110
%rax 0x108
%rdx 456 0x100
Registers Address
123 0x120
%rdi 0x120
0x118
%rsi 0x100
0x110
%rax 123 0x108
%rdx 456 456 0x100
Address Memory
Expression
Computation Address
(%rdx) 0xf000 0xf000
0x8(%rdx) 0xf000 + 0x8 0xf008
(%rdx,%rcx) 0xf000 + 0x100 0xf100
(%rdx,%rcx,4) 0xf000 + 4*0x100 0xf400
0x80(,%rdx,2) 2*0xf000 + 0x80 0x1e080
Outline
• What are assembly/machine instructions
• Registers and operands
• Operations
• Moving data between memory and CPU
• Arithmetic and logical operations
• Control (LEC05)
• Conditional branches
• Loops
• Switch statements
Some arithmetic operations
• Two-Operand Instructions
Format
Computation
addq Src,Dest Dest = Dest + Src
subq Src,Dest Dest = Dest Src
imulq Src,Dest Dest = Dest * Src
salq Src,Dest Dest = Dest << Src Also called shlq
sarq Src,Dest Dest = Dest >> Src Arithmetic
shrq Src,Dest Dest = Dest >> Src Logical
xorq Src,Dest Dest = Dest ^ Src
andq Src,Dest Dest = Dest & Src
orq Src,Dest Dest = Dest | Src
• Watch out for argument order! The result is stored into the Dest register!
• No distinction between signed and unsigned int (why?)
Some arithmetic operations
• One-Operand Instructions
incq Dest Dest = Dest + 1
decq Dest Dest = Dest 1
negq Dest Dest = Dest
notq Dest Dest = ~Dest (bit-
wise inversion)