0% found this document useful (0 votes)
15 views32 pages

CH04-Machine Language I (1)

This document provides an introduction to machine language and assembly instructions as part of a computer systems course. It covers the basics of program execution, the role of the CPU and memory, and the importance of understanding assembly language for programming. The document also outlines the instruction set architecture (ISA) and the operations involved in moving data between memory and the CPU.

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)
15 views32 pages

CH04-Machine Language I (1)

This document provides an introduction to machine language and assembly instructions as part of a computer systems course. It covers the basics of program execution, the role of the CPU and memory, and the importance of understanding assembly language for programming. The document also outlines the instruction set architecture (ISA) and the operations involved in moving data between memory and the CPU.

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/ 32

CH04

Machine Language I
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
• 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)

LEC04, LEC05 LEC06


First impression of assembly/machine code
• Machine instructions have two forms of representation
• Machine Code: Each machine instruction is encoded into a given number
of bytes, the CPU can read these bytes and execute the instruction
• Assembly Code: However, machine instructions in the form of bytes are
hard to read by us human beings, so people designed a text
representation for each machine instruction
Machine instruction example
• C Code
int x = x + y;
• Add two signed integers: x, y
• And store the result in x
• Assembly code
addl (%ebp),%eax • addl: Add two 4-byte integers
• Operands:
x: Register %eax
y: Memory M[%ebp]

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

1940, 5941, 2941: the


Instruction 2: machine code of an
Add two data instruction
items
0003, 0002, 0005: the
source data and the
computed result
Instruction 3:
Write the added
result to the
main memory
Instruction Set Architecture – ISA
• Machine instructions are also a “language”, different computers
may use different set of machine instructions (like talking
different languages)
• Instruction set architecture – ISA
• an ISA defines
• a set of instructions
• data types supported
• hardware features required to execute the instructions, including the
registers, memory access, input/output, etc.
• If two computers supporting the same ISA, in the general sense, the
machine code compiled on one computer can be executed on the other
computer
• We will mainly focus on the x86-64 architecture
Why learning assembly/machine language?
• Many years ago, people write programs with machine code
• Nowadays, people write programs with high-level languages, because they are
easy to understand by human beings
• While the syntax/semantics of high-level languages are far from computer
hardware
• compilers do the dirty work (high-level code  machine code)

• To understand the principles of computers and software programs


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
Machine instruction example
• C Code
int x = x + y;
• Add two signed integers: x, y
• And store the result in x

• 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

CPU Main memory


X86-64 registers
63 31 15 7 0

%rax %eax %ax %al

%rbx %ebx %bx %bl

%rcx %ecx %cx %cl

%rdx %edx %dx %dl

%rsi %esi %si %sil

%rdi %edi %di %dil

%rbp %ebp %bp %bpl

%rsp %esp %sp %spl


X86-64 registers
63 31 15 7 0

%r8 %r8d %r8w %r8b

%r9 %r9d %r9w %r9b

%r10 %r10d %r10w %r10b

%r11 %r11d %r11w %r11b

%r12 %r12d %r12w %r12b

%r13 %r13d %r13w %r13b

%r14 %r14d %r14w %r14b

%r15 %r15d %r15w %r15b


Using different names
to access different parts of a register

63 31 15 7 0

%rax %eax %ax %al

%rax = 00000000111100110000111100000111 0000000000000000 11111111 00000000


quad word
double word %eax = 0000000000000000 11111111 00000000

word %eax = 11111111 00000000

byte %al = 00000000


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
Moving data in a C program
• Pointers in C language
• Declaring variables
• int a, b, c, d; Address Memory
• Normally, variables are stored in the main
memory, sometimes, variables are also 0x00000010 17 a
mapped to registers
• Value assignment 0x00000014 25 b
• a = 17; b = 25;
• Changing the memory content
0x00000018 17 c
• Declaring pointer variables d
0x0000001C 25
• int *pa, *pb;
• Assigning value to a pointer variable 0x00000020 0x00000010 pa
• pa = &a;
• pb = &b; 0x00000024 0x00000014 pb
• Get the value of a data item pointed by a
pointer variable
• c = *pa;
• d = *pb;
Moving data
• Instructions for moving data
mov* Source, Destination
Direction: from source to destination
Moving different data sizes: movq, movl, movw, movb

• Operands can be … (basically, where data are stored)


• Immediate: Constant integer data
• Example: $0x400, $-533
• Like C constant, prefixed with $ in the assembly code
• Register: One of the 16 integer registers
• Example: %rax, %r13
• But %rsp reserved for special use
• Others have special uses for particular instructions
• Memory: 8 consecutive bytes of memory at address given by register
• Simplest example: (%rax)
• Various other “address modes”
Moving data
• mov* Source, Destination
• Take movq for example, movq means the size of the data item is 8 bytes

Source Destination Instruction Equivalent C code Notes

Memory Register temp in %rdx


movq (%rax),%rdx temp = *p;
*p = %rax

Register Memory temp in %rax


movq %rax,(%rdx) *p = temp;
*p = %rdx

Register Register temp1 in %rax


movq %rax,%rdx temp2 = temp1;
temp2 in %rdx

Number Register movq $0x4,%rax temp = 4; temp in %rax

Number Memory movq $-147,(%rax) *p = -147; *p = %rax

Cannot do memory-memory transfer with a single instruction!


An example of moving data
Swapping the data in two different memory addresses

void swap(long *xp, long *yp)


{ xp : the address of the first data item
long t0 = *xp;
yp : the address of the second data item
long t1 = *yp;
*xp = t1; *xp : the first data item
*yp = t0; *yp : the second data item
}

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

swap: Register Value


movq (%rdi), %rax # t0 = *xp %rdi xp
movq (%rsi), %rdx # t1 = *yp %rsi 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 123 0x108
%rdx 456 0x100

swap: Register Value


movq (%rdi), %rax # t0 = *xp %rdi xp
movq (%rsi), %rdx # t1 = *yp %rsi yp
movq %rdx, (%rdi) # *xp = t1 %rax t0
movq %rax, (%rsi) # *yp = t0 %rdx t1
ret
An example of data moving

Registers Address
123 0x120
%rdi 0x120
0x118
%rsi 0x100
0x110
%rax 123 0x108
%rdx 456 456 0x100

swap: Register Value


movq (%rdi), %rax # t0 = *xp %rdi xp
movq (%rsi), %rdx # t1 = *yp %rsi yp
movq %rdx, (%rdi) # *xp = t1 %rax t0
movq %rax, (%rsi) # *yp = t0 %rdx t1
ret
An example of data moving
Memory
Registers Address
456 0x120
%rdi 0x120
0x118
%rsi 0x100
0x110
%rax 123 0x108
%rdx 456 456 0x100

swap: Register Value


movq (%rdi), %rax # t0 = *xp %rdi xp
movq (%rsi), %rdx # t1 = *yp %rsi yp
movq %rdx, (%rdi) # *xp = t1 %rax t0
movq %rax, (%rsi) # *yp = t0 %rdx t1
ret
An example of data moving
Memory
Registers Address
456 0x120
%rdi 0x120
0x118
%rsi 0x100
0x110
%rax 123 0x108
%rdx 456 123 0x100

swap: Register Value


movq (%rdi), %rax # t0 = *xp %rdi xp
movq (%rsi), %rdx # t1 = *yp %rsi yp
movq %rdx, (%rdi) # *xp = t1 %rax t0
movq %rax, (%rsi) # *yp = t0 %rdx t1
ret
Memory addressing modes
• Multiple expressions to describe a memory address
• General Form: D(Rb,Ri,S)  address = Reg[Rb] + S*Reg[Ri] + D
• D: Constant “displacement” 1, 2, or 4 bytes
• Rb: Base register: Any of 16 integer registers%rdx 0xf000
• Ri: Index register: Any, except for %rsp %rcx 0x0100
• S: Scale: 1, 2, 4, or 8

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)

• Note that executing different instructions takes different time


• Please check the costs of instructions here
https://www.agner.org/optimize/instruction_tables.pdf
(see pages 222-236 for Intel Broadwell CPU)
Thank You

You might also like