Download - Machine Program: Basics
![Page 1: Machine Program: Basics](https://reader036.vdocuments.site/reader036/viewer/2022062420/62ae9ebb6645441612563524/html5/thumbnails/1.jpg)
MachineProgram:Basics
Zhaoguo Wang
![Page 2: Machine Program: Basics](https://reader036.vdocuments.site/reader036/viewer/2022062420/62ae9ebb6645441612563524/html5/thumbnails/2.jpg)
Your mental model
CPU
Memory ……
0x00…0058 0x00…0050
…
0x00…0010 0x00…0018 0x00…0020 0x00…0028 0x00…0030
0x00…0038 0x00…0040 0x00…0048
instruction instruction instruction instruction
data data data data
fetch instruction
instruction
64 bit machine (default setting in this lecture)
![Page 3: Machine Program: Basics](https://reader036.vdocuments.site/reader036/viewer/2022062420/62ae9ebb6645441612563524/html5/thumbnails/3.jpg)
Your mental model
CPU
Memory …… …
instruction instruction instruction instruction
data data data data
fetch instruction
instruction
addr
0x00…0058 0x00…0050
0x00…0010 0x00…0018 0x00…0020 0x00…0028 0x00…0030
0x00…0038 0x00…0040 0x00…0048
![Page 4: Machine Program: Basics](https://reader036.vdocuments.site/reader036/viewer/2022062420/62ae9ebb6645441612563524/html5/thumbnails/4.jpg)
Your mental model
CPU
Memory …… …
instruction instruction instruction instruction
data data data data
fetch instruction
instruction
addr
data data
0x00…0058 0x00…0050
0x00…0010 0x00…0018 0x00…0020 0x00…0028 0x00…0030
0x00…0038 0x00…0040 0x00…0048
![Page 5: Machine Program: Basics](https://reader036.vdocuments.site/reader036/viewer/2022062420/62ae9ebb6645441612563524/html5/thumbnails/5.jpg)
Your mental model
CPU
Memory …… …
instruction instruction instruction instruction
data data data data
fetch instruction
instruction
addr
data data
Munachiso’sQues.onsHowdoesCPUknowwhichinstruc9ontofetch?
WheredoesCPUkeeptheinstruc9onanddata?
0x00…0058 0x00…0050
0x00…0010 0x00…0018 0x00…0020 0x00…0028 0x00…0030
0x00…0038 0x00…0040 0x00…0048
![Page 6: Machine Program: Basics](https://reader036.vdocuments.site/reader036/viewer/2022062420/62ae9ebb6645441612563524/html5/thumbnails/6.jpg)
Register – temporary storage area built into a CPU
PC: Program counter – Store memory address of next instruction – Also called “RIP” in x86_64
IR: instruction register – Store the fetched instruction
General purpose registers: – Store operands and pointers used by program
Program status and control register: – Status of the program being executed – All called “EFLAGS” in x86_64
![Page 7: Machine Program: Basics](https://reader036.vdocuments.site/reader036/viewer/2022062420/62ae9ebb6645441612563524/html5/thumbnails/7.jpg)
Register – temporary storage area built into a CPU
PC: Program counter – Store memory address of next instruction – Also called “RIP” in x86_64
IR: instruction register – Store the fetched instruction
General purpose registers: – Store operands and pointers used by program
Program status and control register: – Status of the program being executed – All called “EFLAGS” in x86_64 Programmervisible
![Page 8: Machine Program: Basics](https://reader036.vdocuments.site/reader036/viewer/2022062420/62ae9ebb6645441612563524/html5/thumbnails/8.jpg)
Your mental model
Memory …… …
instruction instruction instruction instruction
data data data data
addr CPU
0x00…0058 PC:
IR:
GPRs:
…
0x00…0058 0x00…0050
0x00…0010 0x00…0018 0x00…0020 0x00…0028 0x00…0030
0x00…0038 0x00…0040 0x00…0048
![Page 9: Machine Program: Basics](https://reader036.vdocuments.site/reader036/viewer/2022062420/62ae9ebb6645441612563524/html5/thumbnails/9.jpg)
Your mental model
Memory …… …
instruction instruction instruction instruction
data data data data
CPU
0x00…0058 PC: instruction
IR: instruction
addr
GPRs:
…
0x00…0058 0x00…0050
0x00…0010 0x00…0018 0x00…0020 0x00…0028 0x00…0030
0x00…0038 0x00…0040 0x00…0048
![Page 10: Machine Program: Basics](https://reader036.vdocuments.site/reader036/viewer/2022062420/62ae9ebb6645441612563524/html5/thumbnails/10.jpg)
Your mental model
Memory …… …
instruction instruction instruction instruction
data data data data
CPU
0x00…0058 PC: instruction
IR: instruction
addr
addr GPRs:
…
0x00…0058 0x00…0050
0x00…0010 0x00…0018 0x00…0020 0x00…0028 0x00…0030
0x00…0038 0x00…0040 0x00…0048
![Page 11: Machine Program: Basics](https://reader036.vdocuments.site/reader036/viewer/2022062420/62ae9ebb6645441612563524/html5/thumbnails/11.jpg)
Your mental model
Memory …… …
instruction instruction instruction instruction
data data data data
CPU
0x00…0058 PC: instruction
IR: instruction
addr
addr
data
GPRs: data
…
0x00…0058 0x00…0050
0x00…0010 0x00…0018 0x00…0020 0x00…0028 0x00…0030
0x00…0038 0x00…0040 0x00…0048
![Page 12: Machine Program: Basics](https://reader036.vdocuments.site/reader036/viewer/2022062420/62ae9ebb6645441612563524/html5/thumbnails/12.jpg)
General Purpose Registers (intel x86-64)
%rsp
%r8
%r9
%r10
%r11
%r12
%r13
%r14
%r15
%rax
%rbx
%rcx
%rdx
%rsi
%rdi
%rbp
8 bytes
![Page 13: Machine Program: Basics](https://reader036.vdocuments.site/reader036/viewer/2022062420/62ae9ebb6645441612563524/html5/thumbnails/13.jpg)
Your mental model
Memory …… …
instruction instruction instruction instruction
data data data data
CPU
0x00…0058 PC: instruction
IR: instruction
addr
addr
data
GPRs: %rax
%rbx
%rcx
%rdx
%rsi
%rdi
%rsp
%rbp …
0x00…0058 0x00…0050
0x00…0010 0x00…0018 0x00…0020 0x00…0028 0x00…0030
0x00…0038 0x00…0040 0x00…0048
![Page 14: Machine Program: Basics](https://reader036.vdocuments.site/reader036/viewer/2022062420/62ae9ebb6645441612563524/html5/thumbnails/14.jpg)
General Purpose Registers (intel x86-64)
%rsp
%r8
%r9
%r10
%r11
%r12
%r13
%r14
%r15
%rax
%rbx
%rcx
%rdx
%rsi
%rdi
%rbp
8 bytes
%eax
%ebx
%ecx
%edx
%esi
%r8d
%r9d
%r10d
%r11d
%r12d
%edi
%esp
%ebp
%r13d
%r14d
%r15d
4 bytes
![Page 15: Machine Program: Basics](https://reader036.vdocuments.site/reader036/viewer/2022062420/62ae9ebb6645441612563524/html5/thumbnails/15.jpg)
Use %rax and %rbx as examples
%rax %eax
8 bytes 4 bytes
%ax
2 bytes
![Page 16: Machine Program: Basics](https://reader036.vdocuments.site/reader036/viewer/2022062420/62ae9ebb6645441612563524/html5/thumbnails/16.jpg)
Use %rax and %rbx as examples
%rax %eax
8 bytes 4 bytes
%ax
2 bytes
%rax %eax %ah %al
1 byte
![Page 17: Machine Program: Basics](https://reader036.vdocuments.site/reader036/viewer/2022062420/62ae9ebb6645441612563524/html5/thumbnails/17.jpg)
Your mental model (intel x86-64)
Memory …… …
instruction instruction instruction instruction
data data data data
CPU
0x00…0058 PC: instruction
IR: instruction
addr
addr
data
GPRs: %rax
%rbx
%rcx
%rdx
%rsi
%rdi
%rsp
%rbp …
0x00…0058 0x00…0050
0x00…0010 0x00…0018 0x00…0020 0x00…0028 0x00…0030
0x00…0038 0x00…0040 0x00…0048
![Page 18: Machine Program: Basics](https://reader036.vdocuments.site/reader036/viewer/2022062420/62ae9ebb6645441612563524/html5/thumbnails/18.jpg)
Steps
1. PC contains the instruction’s address
2. Fetch the instruction into IR
3. Execute the instruction
![Page 19: Machine Program: Basics](https://reader036.vdocuments.site/reader036/viewer/2022062420/62ae9ebb6645441612563524/html5/thumbnails/19.jpg)
Moving data
movq Source, Dest – Copy a quadword (64 bits) from the source operand
(first operand) to the destination operand (second operand).
![Page 20: Machine Program: Basics](https://reader036.vdocuments.site/reader036/viewer/2022062420/62ae9ebb6645441612563524/html5/thumbnails/20.jpg)
Moving data
movq Source, Dest – Copy a quadword (64 bits) from the source operand
(first operand) to the destination operand (second operand).
Iamwai9ngforyourques9onsJ
![Page 21: Machine Program: Basics](https://reader036.vdocuments.site/reader036/viewer/2022062420/62ae9ebb6645441612563524/html5/thumbnails/21.jpg)
Moving data
movq Source, Dest – Copy a quadword (64 bits) from the source operand
(first operand) to the destination operand (second operand).
– In Intel x86 world, the instruction set architecture (ISA) uses 16 bits as a word. (different from what we have learnt in C)
• 8086 uses 16 bits as a word • Support full backward compatibility
– Process the same binary executable software instructions as their predecessors
– Allowing the use of a newer processor without having to acquire new applications or operating systems.
![Page 22: Machine Program: Basics](https://reader036.vdocuments.site/reader036/viewer/2022062420/62ae9ebb6645441612563524/html5/thumbnails/22.jpg)
Moving data
movq Source, Dest
Operand Types – Immediate: Constant integer data
• Prefixed with $ • Example: $0x400, $-533
– Register: One of general purpose registers • Example: %rax, %rsi
– Memory: 8 consecutive bytes of memory • Indexed by register with various “address modes” • Simplest example: (%rax)
![Page 23: Machine Program: Basics](https://reader036.vdocuments.site/reader036/viewer/2022062420/62ae9ebb6645441612563524/html5/thumbnails/23.jpg)
movq Operand combinations
movq
Imm
Reg
Mem
Reg Mem
Reg Mem
Reg
Source Dest
movq$0x4,%rax
movq$0x4,(%rax)
movq%rax,%rdx
movq%rax,(%rdx)
movq(%rax),%rdx
Source, Dest
2. Cannot do memory-memory transfer with a single instruction
1. Immediate can only be Source
![Page 24: Machine Program: Basics](https://reader036.vdocuments.site/reader036/viewer/2022062420/62ae9ebb6645441612563524/html5/thumbnails/24.jpg)
movq Imm, Reg
Memory
movq%rax,%rbx
…… …
CPU
0x00…0050 PC:
IR:
RAX:
…
RBX:
RCX:
RDX:
RSI:
RDI:
RSP:
RBP:
movq$0x4,%rax
0x00…0058 0x00…0050
0x00…0010 0x00…0018 0x00…0020 0x00…0028 0x00…0030
0x00…0038 0x00…0040 0x00…0048
PC
![Page 25: Machine Program: Basics](https://reader036.vdocuments.site/reader036/viewer/2022062420/62ae9ebb6645441612563524/html5/thumbnails/25.jpg)
movq Imm, Reg
Memory
movq%rax,%rbx
…… …
CPU
0x00…0050 PC:
IR:
RAX:
…
RBX:
RCX:
RDX:
RSI:
RDI:
RSP:
RBP:
movq $0x4, %rax
movq$0x4,%rax
0x00…0058 0x00…0050
0x00…0010 0x00…0018 0x00…0020 0x00…0028 0x00…0030
0x00…0038 0x00…0040 0x00…0048
PC
![Page 26: Machine Program: Basics](https://reader036.vdocuments.site/reader036/viewer/2022062420/62ae9ebb6645441612563524/html5/thumbnails/26.jpg)
movq Imm, Reg CPU
0x00…0050 PC:
IR:
RAX: 0x00…0004
…
RBX:
RCX:
RDX:
RSI:
RDI:
RSP:
RBP: Memory
movq$0x4,%rax
movq%rax,%rbx
…… …
movq $0x4, %rax
0x00…0058 0x00…0050
0x00…0010 0x00…0018 0x00…0020 0x00…0028 0x00…0030
0x00…0038 0x00…0040 0x00…0048
PC
![Page 27: Machine Program: Basics](https://reader036.vdocuments.site/reader036/viewer/2022062420/62ae9ebb6645441612563524/html5/thumbnails/27.jpg)
Steps
1. PC contains the instruction’s address
2. Load the instruction into IR
3. Execute the instruction 4. CPU automatically updates PC after current instruction finishes (is retired).
![Page 28: Machine Program: Basics](https://reader036.vdocuments.site/reader036/viewer/2022062420/62ae9ebb6645441612563524/html5/thumbnails/28.jpg)
movq Reg, Reg
Memory
movq$0x4,%rax
movq%rax,%rbx
…… …
CPU
0x00…0058 PC:
IR:
RAX: 0x00…0004
…
RBX:
RCX:
RDX:
RSI:
RDI:
RSP:
RBP:
movq $0x4, %rax
0x00…0058 0x00…0050
0x00…0010 0x00…0018 0x00…0020 0x00…0028 0x00…0030
0x00…0038 0x00…0040 0x00…0048
PC
![Page 29: Machine Program: Basics](https://reader036.vdocuments.site/reader036/viewer/2022062420/62ae9ebb6645441612563524/html5/thumbnails/29.jpg)
movq Reg, Reg
Memory
movq$0x4,%rax
movq%rax,%rbx
…… …
CPU
0x00…0058 PC:
IR:
RAX: 0x00…0004
…
RBX:
RCX:
RDX:
RSI:
RDI:
RSP:
RBP:
movq %rax, %rbx
0x00…0058 0x00…0050
0x00…0010 0x00…0018 0x00…0020 0x00…0028 0x00…0030
0x00…0038 0x00…0040 0x00…0048
PC
![Page 30: Machine Program: Basics](https://reader036.vdocuments.site/reader036/viewer/2022062420/62ae9ebb6645441612563524/html5/thumbnails/30.jpg)
movq Reg, Reg
Memory
movq$0x4,%rax
movq%rax,%rbx
…… …
CPU
0x00…0058 PC:
IR:
RAX: 0x00…0004
0x00…0004
…
RBX:
RCX:
RDX:
RSI:
RDI:
RSP:
RBP:
movq %rax, %rbx
0x00…0058 0x00…0050
0x00…0010 0x00…0018 0x00…0020 0x00…0028 0x00…0030
0x00…0038 0x00…0040 0x00…0048
PC
![Page 31: Machine Program: Basics](https://reader036.vdocuments.site/reader036/viewer/2022062420/62ae9ebb6645441612563524/html5/thumbnails/31.jpg)
movq Mem, Reg
How to represent Mem?
![Page 32: Machine Program: Basics](https://reader036.vdocuments.site/reader036/viewer/2022062420/62ae9ebb6645441612563524/html5/thumbnails/32.jpg)
Strawman: directly use memory address
movq (0x00…000a), %rbx
![Page 33: Machine Program: Basics](https://reader036.vdocuments.site/reader036/viewer/2022062420/62ae9ebb6645441612563524/html5/thumbnails/33.jpg)
Strawman: directly use memory address
movq (0x00…000a), %rbx – An instruction is 64 bits long, – At lease 1 byte to represent the opcode – do not have enough bits to represent memory
address
![Page 34: Machine Program: Basics](https://reader036.vdocuments.site/reader036/viewer/2022062420/62ae9ebb6645441612563524/html5/thumbnails/34.jpg)
Basic Idea: use registers to index the memory
(Register) – The content of the register specifies memory address – movq (%rax), %rbx
![Page 35: Machine Program: Basics](https://reader036.vdocuments.site/reader036/viewer/2022062420/62ae9ebb6645441612563524/html5/thumbnails/35.jpg)
movq (%rax), %rbx
Memory
movq (%rax), %rbx
0x10
…… …
CPU
0x00…0058 PC:
IR:
RAX: 0x18
…
RBX:
RCX:
RDX:
RSI:
RDI:
RSP:
RBP:
0x00…0058 0x00…0050
0x00…0010 0x00…0018 0x00…0020 0x00…0028 0x00…0030
0x00…0038 0x00…0040 0x00…0048
PC
![Page 36: Machine Program: Basics](https://reader036.vdocuments.site/reader036/viewer/2022062420/62ae9ebb6645441612563524/html5/thumbnails/36.jpg)
movq (%rax), %rbx
Memory
movq (%rax), %rbx
0x10
…… …
CPU
0x00…0058 PC:
IR:
RAX: 0x18
…
RBX:
RCX:
RDX:
RSI:
RDI:
RSP:
RBP:
movq (%rax), %rbx
0x00…0058 0x00…0050
0x00…0010 0x00…0018 0x00…0020 0x00…0028 0x00…0030
0x00…0038 0x00…0040 0x00…0048
PC
![Page 37: Machine Program: Basics](https://reader036.vdocuments.site/reader036/viewer/2022062420/62ae9ebb6645441612563524/html5/thumbnails/37.jpg)
movq (%rax), %rbx
Memory
movq (%rax), %rbx
0x10
…… …
CPU
0x00…0058 PC:
IR:
RAX: 0x18
…
RBX:
RCX:
RDX:
RSI:
RDI:
RSP:
RBP:
movq (%rax), %rbx 0x00…0018
0x00…0058 0x00…0050
0x00…0010 0x00…0018 0x00…0020 0x00…0028 0x00…0030
0x00…0038 0x00…0040 0x00…0048
PC
![Page 38: Machine Program: Basics](https://reader036.vdocuments.site/reader036/viewer/2022062420/62ae9ebb6645441612563524/html5/thumbnails/38.jpg)
movq (%rax), %rbx
Memory
movq (%rax), %rbx
0x10
…… …
CPU
0x00…0058 PC:
IR:
RAX: 0x18
0x10
…
RBX:
RCX:
RDX:
RSI:
RDI:
RSP:
RBP:
movq (%rax), %rbx 0x00…0018
0x10
0x00…0058 0x00…0050
0x00…0010 0x00…0018 0x00…0020 0x00…0028 0x00…0030
0x00…0038 0x00…0040 0x00…0048
PC
![Page 39: Machine Program: Basics](https://reader036.vdocuments.site/reader036/viewer/2022062420/62ae9ebb6645441612563524/html5/thumbnails/39.jpg)
swap function voidswap(long*a,long*b){longtmp=*a;*a=*b;*b=tmp;}
swap:
GCC –S –O3 swap.c
![Page 40: Machine Program: Basics](https://reader036.vdocuments.site/reader036/viewer/2022062420/62ae9ebb6645441612563524/html5/thumbnails/40.jpg)
swap function voidswap(long*a,long*b){longtmp=*a;*a=*b;*b=tmp;}
swap:movq(%rdi),%raxmovq(%rsi),%rdxmovq%rdx,(%rdi)movq%rax,(%rsi)
GCC –S –O3 swap.c
![Page 41: Machine Program: Basics](https://reader036.vdocuments.site/reader036/viewer/2022062420/62ae9ebb6645441612563524/html5/thumbnails/41.jpg)
swap func
Memory
movq (%rsi), %rdx
movq %rdx, (%rdi)
movq %rax, (%rsi)
movq (%rdi), %rax
0x00…0018
0x00…0010
0x2
0x1
…… …
CPU
0x00…0048 PC:
IR:
RAX:
0x00…0018
0x00…0010
…
RBX:
RCX:
RDX:
RSI:
RDI:
RSP:
RBP:
int *a
int *b
PC
0x00…0058 0x00…0050
0x00…0010 0x00…0018 0x00…0020 0x00…0028 0x00…0030
0x00…0038 0x00…0040 0x00…0048
0x00…0060
![Page 42: Machine Program: Basics](https://reader036.vdocuments.site/reader036/viewer/2022062420/62ae9ebb6645441612563524/html5/thumbnails/42.jpg)
swap func
Memory
movq (%rsi), %rdx
movq %rdx, (%rdi)
movq %rax, (%rsi)
movq (%rdi), %rax
0x00…0018
0x00…0010
0x2
0x1
…… …
CPU
0x00…0048 PC:
IR:
RAX:
0x00…0018
0x00…0010
…
RBX:
RCX:
RDX:
RSI:
RDI:
RSP:
RBP:
movq (%rdi), %rax
PC
0x00…0058 0x00…0050
0x00…0010 0x00…0018 0x00…0020 0x00…0028 0x00…0030
0x00…0038 0x00…0040 0x00…0048
0x00…0060
![Page 43: Machine Program: Basics](https://reader036.vdocuments.site/reader036/viewer/2022062420/62ae9ebb6645441612563524/html5/thumbnails/43.jpg)
swap func
Memory
movq (%rsi), %rdx
movq %rdx, (%rdi)
movq %rax, (%rsi)
movq (%rdi), %rax
0x00…0018
0x00…0010
0x2
0x1
…… …
CPU
0x00…0048 PC:
IR:
RAX: 0x1
0x00…0018
0x00…0010
…
RBX:
RCX:
RDX:
RSI:
RDI:
RSP:
RBP:
movq (%rdi), %rax
PC
0x00…0058 0x00…0050
0x00…0010 0x00…0018 0x00…0020 0x00…0028 0x00…0030
0x00…0038 0x00…0040 0x00…0048
0x00…0060
![Page 44: Machine Program: Basics](https://reader036.vdocuments.site/reader036/viewer/2022062420/62ae9ebb6645441612563524/html5/thumbnails/44.jpg)
swap func
Memory
movq (%rsi), %rdx
movq %rdx, (%rdi)
movq %rax, (%rsi)
movq (%rdi), %rax
0x00…0018
0x00…0010
0x2
0x1
…… …
CPU
0x00…0050 PC:
IR:
RAX: 0x1
0x00…0018
0x00…0010
…
RBX:
RCX:
RDX:
RSI:
RDI:
RSP:
RBP:
movq (%rsi), %rdx PC
0x00…0058 0x00…0050
0x00…0010 0x00…0018 0x00…0020 0x00…0028 0x00…0030
0x00…0038 0x00…0040 0x00…0048
0x00…0060
![Page 45: Machine Program: Basics](https://reader036.vdocuments.site/reader036/viewer/2022062420/62ae9ebb6645441612563524/html5/thumbnails/45.jpg)
swap func
Memory
movq (%rsi), %rdx
movq %rdx, (%rdi)
movq %rax, (%rsi)
movq (%rdi), %rax
0x00…0018
0x00…0010
0x2
0x1
…… …
CPU
0x00…0050 PC:
IR:
RAX: 0x1
0x2
0x00…0018
0x00…0010
…
RBX:
RCX:
RDX:
RSI:
RDI:
RSP:
RBP:
movq (%rsi), %rdx PC
0x00…0058 0x00…0050
0x00…0010 0x00…0018 0x00…0020 0x00…0028 0x00…0030
0x00…0038 0x00…0040 0x00…0048
0x00…0060
![Page 46: Machine Program: Basics](https://reader036.vdocuments.site/reader036/viewer/2022062420/62ae9ebb6645441612563524/html5/thumbnails/46.jpg)
swap func
Memory
movq (%rsi), %rdx
movq %rdx, (%rdi)
movq %rax, (%rsi)
movq (%rdi), %rax
0x00…0018
0x00…0010
0x2
0x1
…… …
CPU
0x00…0058 PC:
IR:
RAX: 0x1
0x2
0x00…0018
0x00…0010
…
RBX:
RCX:
RDX:
RSI:
RDI:
RSP:
RBP:
movq %rdx, (%rdi)
PC 0x00…0058 0x00…0050
0x00…0010 0x00…0018 0x00…0020 0x00…0028 0x00…0030
0x00…0038 0x00…0040 0x00…0048
0x00…0060
![Page 47: Machine Program: Basics](https://reader036.vdocuments.site/reader036/viewer/2022062420/62ae9ebb6645441612563524/html5/thumbnails/47.jpg)
swap func
Memory
movq (%rsi), %rdx
movq %rdx, (%rdi)
movq %rax, (%rsi)
movq (%rdi), %rax
0x00…0018
0x00…0010
0x2
0x2
…… …
CPU
0x00…0058 PC:
IR:
RAX: 0x1
0x2
0x00…0018
0x00…0010
…
RBX:
RCX:
RDX:
RSI:
RDI:
RSP:
RBP:
movq %rdx, (%rdi)
PC 0x00…0058 0x00…0050
0x00…0010 0x00…0018 0x00…0020 0x00…0028 0x00…0030
0x00…0038 0x00…0040 0x00…0048
0x00…0060
![Page 48: Machine Program: Basics](https://reader036.vdocuments.site/reader036/viewer/2022062420/62ae9ebb6645441612563524/html5/thumbnails/48.jpg)
swap func
Memory
movq (%rsi), %rdx
movq %rdx, (%rdi)
movq %rax, (%rsi)
movq (%rdi), %rax
0x00…0018
0x00…0010
0x2
0x2
…… …
CPU
0x00…0060 PC:
IR:
RAX: 0x1
0x2
0x00…0018
0x00…0010
…
RBX:
RCX:
RDX:
RSI:
RDI:
RSP:
RBP:
movq %rax, (%rsi)
PC
0x00…0058 0x00…0050
0x00…0010 0x00…0018 0x00…0020 0x00…0028 0x00…0030
0x00…0038 0x00…0040 0x00…0048
0x00…0060
![Page 49: Machine Program: Basics](https://reader036.vdocuments.site/reader036/viewer/2022062420/62ae9ebb6645441612563524/html5/thumbnails/49.jpg)
swap func
Memory
movq (%rsi), %rdx
movq %rdx, (%rdi)
movq %rax, (%rsi)
movq (%rdi), %rax
0x00…0018
0x00…0010
0x1
0x2
…… …
CPU
0x00…0060 PC:
IR:
RAX: 0x1
0x2
0x00…0018
0x00…0010
…
RBX:
RCX:
RDX:
RSI:
RDI:
RSP:
RBP:
movq %rax, (%rsi)
PC
0x00…0058 0x00…0050
0x00…0010 0x00…0018 0x00…0020 0x00…0028 0x00…0030
0x00…0038 0x00…0040 0x00…0048
0x00…0060
![Page 50: Machine Program: Basics](https://reader036.vdocuments.site/reader036/viewer/2022062420/62ae9ebb6645441612563524/html5/thumbnails/50.jpg)
Issue
Issue: before each memory access – need to calculate the address, and store it into the
register
![Page 51: Machine Program: Basics](https://reader036.vdocuments.site/reader036/viewer/2022062420/62ae9ebb6645441612563524/html5/thumbnails/51.jpg)
Issue
Issue: before each memory access – need to calculate the address, and store it into the
register
longa[]={3,2,1};for(inti=0;i<3;i++){a[i]=i;}
longa[]={1,2,3};for(inti=0;i<3;i++){ 1.put&a[i]intoreg2.mov$i,(reg)}
![Page 52: Machine Program: Basics](https://reader036.vdocuments.site/reader036/viewer/2022062420/62ae9ebb6645441612563524/html5/thumbnails/52.jpg)
Memory
movq $0x20, %rax
movq $0x2, (%rax)
movq $0x1, (%rax)
0x1
0x2
0x3
…… …
movq $0x10, %rax
CPU
0x00…0030 PC:
IR: movq $0x10, %rax
RAX:
…
RBX:
RCX:
RDX:
RSI:
RDI:
RSP:
RBP:
movq $0x0, (%rax)
movq $0x18, %rax
0x00…0058 0x00…0050
0x00…0010 0x00…0018 0x00…0020 0x00…0028 0x00…0030
0x00…0038 0x00…0040 0x00…0048
0x00…0060
PC
a[0] a[1]
a[2]
Example
![Page 53: Machine Program: Basics](https://reader036.vdocuments.site/reader036/viewer/2022062420/62ae9ebb6645441612563524/html5/thumbnails/53.jpg)
Memory
movq $0x20, %rax
movq $0x2, (%rax)
movq $0x1, (%rax)
0x1
0x2
0x3
…… …
movq $0x10, %rax
CPU
0x00…0030 PC:
IR: movq $0x10, %rax
RAX: 0x00…0010
…
RBX:
RCX:
RDX:
RSI:
RDI:
RSP:
RBP:
movq $0x0, (%rax)
movq $0x18, %rax
0x00…0058 0x00…0050
0x00…0010 0x00…0018 0x00…0020 0x00…0028 0x00…0030
0x00…0038 0x00…0040 0x00…0048
0x00…0060
PC
a[0] a[1]
a[2]
Example
![Page 54: Machine Program: Basics](https://reader036.vdocuments.site/reader036/viewer/2022062420/62ae9ebb6645441612563524/html5/thumbnails/54.jpg)
Memory
movq $0x20, %rax
movq $0x2, (%rax)
movq $0x1, (%rax)
0x1
0x2
0x3
…… …
movq $0x10, %rax
CPU
0x00…0038 PC:
IR: movq $0x0, (%rax)
RAX: 0x00…0010
…
RBX:
RCX:
RDX:
RSI:
RDI:
RSP:
RBP:
movq $0x0, (%rax)
movq $0x18, %rax
0x00…0058 0x00…0050
0x00…0010 0x00…0018 0x00…0020 0x00…0028 0x00…0030
0x00…0038 0x00…0040 0x00…0048
0x00…0060
PC
a[0] a[1]
a[2]
Example
![Page 55: Machine Program: Basics](https://reader036.vdocuments.site/reader036/viewer/2022062420/62ae9ebb6645441612563524/html5/thumbnails/55.jpg)
Memory
movq $0x20, %rax
movq $0x2, (%rax)
movq $0x1, (%rax)
0x1
0x2
0x0
…… …
movq $0x10, %rax
CPU
0x00…0038 PC:
IR: movq $0x0, (%rax)
RAX: 0x00…0010
…
RBX:
RCX:
RDX:
RSI:
RDI:
RSP:
RBP:
movq $0x0, (%rax)
movq $0x18, %rax
0x00…0058 0x00…0050
0x00…0010 0x00…0018 0x00…0020 0x00…0028 0x00…0030
0x00…0038 0x00…0040 0x00…0048
0x00…0060
PC
a[0] a[1]
a[2]
Example
![Page 56: Machine Program: Basics](https://reader036.vdocuments.site/reader036/viewer/2022062420/62ae9ebb6645441612563524/html5/thumbnails/56.jpg)
Memory
movq $0x20, %rax
movq $0x2, (%rax)
movq $0x1, (%rax)
0x1
0x2
0x0
…… …
movq $0x10, %rax
CPU
0x00…0040 PC:
IR: movq $0x18, (%rax)
RAX: 0x00…0010
…
RBX:
RCX:
RDX:
RSI:
RDI:
RSP:
RBP:
movq $0x0, (%rax)
movq $0x18, %rax
0x00…0058 0x00…0050
0x00…0010 0x00…0018 0x00…0020 0x00…0028 0x00…0030
0x00…0038 0x00…0040 0x00…0048
0x00…0060
PC
a[0] a[1]
a[2]
Example
![Page 57: Machine Program: Basics](https://reader036.vdocuments.site/reader036/viewer/2022062420/62ae9ebb6645441612563524/html5/thumbnails/57.jpg)
Memory
movq $0x20, %rax
movq $0x2, (%rax)
movq $0x1, (%rax)
0x1
0x2
0x0
…… …
movq $0x10, %rax
CPU
0x00…0040 PC:
IR: movq $0x18, (%rax)
RAX: 0x00…0018
…
RBX:
RCX:
RDX:
RSI:
RDI:
RSP:
RBP:
movq $0x0, (%rax)
movq $0x18, %rax
0x00…0058 0x00…0050
0x00…0010 0x00…0018 0x00…0020 0x00…0028 0x00…0030
0x00…0038 0x00…0040 0x00…0048
0x00…0060
PC
a[0] a[1]
a[2]
Example
![Page 58: Machine Program: Basics](https://reader036.vdocuments.site/reader036/viewer/2022062420/62ae9ebb6645441612563524/html5/thumbnails/58.jpg)
Memory
movq $0x20, %rax
movq $0x2, (%rax)
movq $0x1, (%rax)
0x1
0x2
0x0
…… …
movq $0x10, %rax
CPU
0x00…0048 PC:
IR: movq $0x1, (%rax)
RAX: 0x00…0018
…
RBX:
RCX:
RDX:
RSI:
RDI:
RSP:
RBP:
movq $0x0, (%rax)
movq $0x18, %rax
0x00…0058 0x00…0050
0x00…0010 0x00…0018 0x00…0020 0x00…0028 0x00…0030
0x00…0038 0x00…0040 0x00…0048
0x00…0060
PC
a[0] a[1]
a[2]
Example
![Page 59: Machine Program: Basics](https://reader036.vdocuments.site/reader036/viewer/2022062420/62ae9ebb6645441612563524/html5/thumbnails/59.jpg)
Memory
movq $0x20, %rax
movq $0x2, (%rax)
movq $0x1, (%rax)
0x1
0x1
0x0
…… …
movq $0x10, %rax
CPU
0x00…0048 PC:
IR: movq $0x1, (%rax)
RAX: 0x00…0018
…
RBX:
RCX:
RDX:
RSI:
RDI:
RSP:
RBP:
movq $0x0, (%rax)
movq $0x18, %rax
0x00…0058 0x00…0050
0x00…0010 0x00…0018 0x00…0020 0x00…0028 0x00…0030
0x00…0038 0x00…0040 0x00…0048
0x00…0060
PC
a[0] a[1]
a[2]
Example
![Page 60: Machine Program: Basics](https://reader036.vdocuments.site/reader036/viewer/2022062420/62ae9ebb6645441612563524/html5/thumbnails/60.jpg)
Memory
movq $0x20, %rax
movq $0x2, (%rax)
movq $0x1, (%rax)
0x1
0x1
0x0
…… …
movq $0x10, %rax
CPU
0x00…0050 PC:
IR: movq $0x20, %rax
RAX: 0x00…0018
…
RBX:
RCX:
RDX:
RSI:
RDI:
RSP:
RBP:
movq $0x0, (%rax)
movq $0x18, %rax
0x00…0058 0x00…0050
0x00…0010 0x00…0018 0x00…0020 0x00…0028 0x00…0030
0x00…0038 0x00…0040 0x00…0048
0x00…0060
PC
a[0] a[1]
a[2]
Example
![Page 61: Machine Program: Basics](https://reader036.vdocuments.site/reader036/viewer/2022062420/62ae9ebb6645441612563524/html5/thumbnails/61.jpg)
Memory
movq $0x20, %rax
movq $0x2, (%rax)
movq $0x1, (%rax)
0x1
0x1
0x0
…… …
movq $0x10, %rax
CPU
0x00…0050 PC:
IR: movq $0x20, %rax
RAX: 0x00…0020
…
RBX:
RCX:
RDX:
RSI:
RDI:
RSP:
RBP:
movq $0x0, (%rax)
movq $0x18, %rax
0x00…0058 0x00…0050
0x00…0010 0x00…0018 0x00…0020 0x00…0028 0x00…0030
0x00…0038 0x00…0040 0x00…0048
0x00…0060
PC
a[0] a[1]
a[2]
Example
![Page 62: Machine Program: Basics](https://reader036.vdocuments.site/reader036/viewer/2022062420/62ae9ebb6645441612563524/html5/thumbnails/62.jpg)
Memory
movq $0x20, %rax
movq $0x2, (%rax)
movq $0x1, (%rax)
0x1
0x1
0x0
…… …
movq $0x10, %rax
CPU
0x00…0058 PC:
IR: movq $0x2, (%rax)
RAX: 0x00…0020
…
RBX:
RCX:
RDX:
RSI:
RDI:
RSP:
RBP:
movq $0x0, (%rax)
movq $0x18, %rax
0x00…0058 0x00…0050
0x00…0010 0x00…0018 0x00…0020 0x00…0028 0x00…0030
0x00…0038 0x00…0040 0x00…0048
0x00…0060 PC
a[0] a[1]
a[2]
Example
![Page 63: Machine Program: Basics](https://reader036.vdocuments.site/reader036/viewer/2022062420/62ae9ebb6645441612563524/html5/thumbnails/63.jpg)
Memory
movq $0x20, %rax
movq $0x2, (%rax)
movq $0x1, (%rax)
0x2
0x1
0x0
…… …
movq $0x10, %rax
CPU
0x00…0058 PC:
IR: movq $0x2, (%rax)
RAX: 0x00…0020
…
RBX:
RCX:
RDX:
RSI:
RDI:
RSP:
RBP:
movq $0x0, (%rax)
movq $0x18, %rax
0x00…0058 0x00…0050
0x00…0010 0x00…0018 0x00…0020 0x00…0028 0x00…0030
0x00…0038 0x00…0040 0x00…0048
0x00…0060 PC
a[0] a[1]
a[2]
Example
![Page 64: Machine Program: Basics](https://reader036.vdocuments.site/reader036/viewer/2022062420/62ae9ebb6645441612563524/html5/thumbnails/64.jpg)
Observation longa[]={3,2,1};for(inti=0;i<3;i++){a[i]=i;}
a[0], a[1] and a[2] have the same base address:&a[0] – &a[0]: &a[0] + 0 – &a[1]: &a[0] + 1 – &a[2]: &a[0] + 2
![Page 65: Machine Program: Basics](https://reader036.vdocuments.site/reader036/viewer/2022062420/62ae9ebb6645441612563524/html5/thumbnails/65.jpg)
Address mode with displacement
D(Register): val(Register) + D – Register specifies the start of the memory region – Constant D specifies the offset
0x10 RAX:
20(RAX)
0x10
20
+ RAX:
0x24
![Page 66: Machine Program: Basics](https://reader036.vdocuments.site/reader036/viewer/2022062420/62ae9ebb6645441612563524/html5/thumbnails/66.jpg)
Address mode with displacement
longa[]={3,2,1};for(inti=0;i<3;i++){a[i]=i;}
longa[]={1,2,3};for(inti=0;i<3;i++){
mov$i,D(reg);//D=i*8,reg=&a[0]}
D(Register): val(Register) + D – Register specifies the start of the memory region – Constant D specifies the offset
![Page 67: Machine Program: Basics](https://reader036.vdocuments.site/reader036/viewer/2022062420/62ae9ebb6645441612563524/html5/thumbnails/67.jpg)
Example
Memory
movq $0x2, 16(%rax)
0x1
0x2
0x3
…… …
movq $0x10, %rax
CPU
0x00…0030 PC:
IR: movq $0x10, %rax
RAX:
…
RBX:
RCX:
RDX:
RSI:
RDI:
RSP:
RBP:
movq $0x0, (%rax)
movq $0x1, 8(%rax)
0x00…0058 0x00…0050
0x00…0010 0x00…0018 0x00…0020 0x00…0028 0x00…0030
0x00…0038 0x00…0040 0x00…0048
0x00…0060
PC
a[0] a[1]
a[2]
![Page 68: Machine Program: Basics](https://reader036.vdocuments.site/reader036/viewer/2022062420/62ae9ebb6645441612563524/html5/thumbnails/68.jpg)
Example
Memory
movq $0x2, 16(%rax)
0x1
0x2
0x3
…… …
movq $0x10, %rax
CPU
0x00…0030 PC:
IR: movq $0x10, %rax
RAX: 0x00…0010
…
RBX:
RCX:
RDX:
RSI:
RDI:
RSP:
RBP:
movq $0x0, (%rax)
movq $0x1, 8(%rax)
0x00…0058 0x00…0050
0x00…0010 0x00…0018 0x00…0020 0x00…0028 0x00…0030
0x00…0038 0x00…0040 0x00…0048
0x00…0060
PC
a[0] a[1]
a[2]
![Page 69: Machine Program: Basics](https://reader036.vdocuments.site/reader036/viewer/2022062420/62ae9ebb6645441612563524/html5/thumbnails/69.jpg)
Example
Memory
movq $0x2, 16(%rax)
0x1
0x2
0x3
…… …
movq $0x10, %rax
CPU
0x00…0038 PC:
IR: movq $0x0, (%rax)
RAX: 0x00…0010
…
RBX:
RCX:
RDX:
RSI:
RDI:
RSP:
RBP:
movq $0x0, (%rax)
movq $0x1, 8(%rax)
0x00…0058 0x00…0050
0x00…0010 0x00…0018 0x00…0020 0x00…0028 0x00…0030
0x00…0038 0x00…0040 0x00…0048
0x00…0060
PC
a[0] a[1]
a[2]
![Page 70: Machine Program: Basics](https://reader036.vdocuments.site/reader036/viewer/2022062420/62ae9ebb6645441612563524/html5/thumbnails/70.jpg)
Example
Memory
movq $0x2, 16(%rax)
0x1
0x2
0x0
…… …
movq $0x10, %rax
CPU
0x00…0038 PC:
IR: movq $0x0, (%rax)
RAX: 0x00…0010
…
RBX:
RCX:
RDX:
RSI:
RDI:
RSP:
RBP:
movq $0x0, (%rax)
movq $0x1, 8(%rax)
0x00…0058 0x00…0050
0x00…0010 0x00…0018 0x00…0020 0x00…0028 0x00…0030
0x00…0038 0x00…0040 0x00…0048
0x00…0060
PC
a[0] a[1]
a[2]
![Page 71: Machine Program: Basics](https://reader036.vdocuments.site/reader036/viewer/2022062420/62ae9ebb6645441612563524/html5/thumbnails/71.jpg)
Example
Memory
movq $0x2, 16(%rax)
0x1
0x2
0x0
…… …
movq $0x10, %rax
CPU
0x00…0040 PC:
IR: movq $0x1, 8(%rax)
RAX: 0x00…0010
…
RBX:
RCX:
RDX:
RSI:
RDI:
RSP:
RBP:
movq $0x0, (%rax)
movq $0x1, 8(%rax)
0x00…0058 0x00…0050
0x00…0010 0x00…0018 0x00…0020 0x00…0028 0x00…0030
0x00…0038 0x00…0040 0x00…0048
0x00…0060
PC
a[0] a[1]
a[2]
![Page 72: Machine Program: Basics](https://reader036.vdocuments.site/reader036/viewer/2022062420/62ae9ebb6645441612563524/html5/thumbnails/72.jpg)
Example
Memory
movq $0x2, 16(%rax)
0x1
0x1
0x0
…… …
movq $0x10, %rax
CPU
0x00…0040 PC:
IR: movq $0x1, 8(%rax)
RAX: 0x00…0010
…
RBX:
RCX:
RDX:
RSI:
RDI:
RSP:
RBP:
movq $0x0, (%rax)
movq $0x1, 8(%rax)
0x00…0058 0x00…0050
0x00…0010 0x00…0018 0x00…0020 0x00…0028 0x00…0030
0x00…0038 0x00…0040 0x00…0048
0x00…0060
PC
a[0] a[1]
a[2]
![Page 73: Machine Program: Basics](https://reader036.vdocuments.site/reader036/viewer/2022062420/62ae9ebb6645441612563524/html5/thumbnails/73.jpg)
Example
Memory
movq $0x2, 16(%rax)
0x1
0x1
0x0
…… …
movq $0x10, %rax
CPU
0x00…0048 PC:
IR: movq $0x2, 16(%rax)
RAX: 0x00…0010
…
RBX:
RCX:
RDX:
RSI:
RDI:
RSP:
RBP:
movq $0x0, (%rax)
movq $0x1, 8(%rax)
0x00…0058 0x00…0050
0x00…0010 0x00…0018 0x00…0020 0x00…0028 0x00…0030
0x00…0038 0x00…0040 0x00…0048
0x00…0060
PC
a[0] a[1]
a[2]
![Page 74: Machine Program: Basics](https://reader036.vdocuments.site/reader036/viewer/2022062420/62ae9ebb6645441612563524/html5/thumbnails/74.jpg)
Example
Memory
movq $0x2, 16(%rax)
0x2
0x1
0x0
…… …
movq $0x10, %rax
CPU
0x00…0048 PC:
IR: movq $0x2, 16(%rax)
RAX: 0x00…0010
…
RBX:
RCX:
RDX:
RSI:
RDI:
RSP:
RBP:
movq $0x0, (%rax)
movq $0x1, 8(%rax)
0x00…0058 0x00…0050
0x00…0010 0x00…0018 0x00…0020 0x00…0028 0x00…0030
0x00…0038 0x00…0040 0x00…0048
0x00…0060
PC
a[0] a[1]
a[2]
![Page 75: Machine Program: Basics](https://reader036.vdocuments.site/reader036/viewer/2022062420/62ae9ebb6645441612563524/html5/thumbnails/75.jpg)
Complete Memory Addressing Mode
D(Rb, Ri, S): val(Rb) + S * val(Ri) + D – Rb: Base register – D: Constant “displacement” – Ri: Index register (not%rsp)– S: Scale: 1, 2, 4, or 8
0x8
4
* rbx:
0x3a
0x10 rax:
S:
+
0x20
10 D:
10(%rax, %rbx, 4)
![Page 76: Machine Program: Basics](https://reader036.vdocuments.site/reader036/viewer/2022062420/62ae9ebb6645441612563524/html5/thumbnails/76.jpg)
Complete Memory Addressing Mode
D(Rb, Ri, S): val(Rb) + S * val(Ri) + D – D: Constant “displacement” – Rb: Base register – Ri: Index register (not%rsp)– S: Scale: 1, 2, 4, or 8
If S is 1 or D is 0, we can just get rid of them – (Rb, Ri): val(Rb) + val(Ri) – D(Rb, Ri): val(Rb) + val(Ri) + D – (Rb, Ri, S): val(Rb) + S * val(Ri)
![Page 77: Machine Program: Basics](https://reader036.vdocuments.site/reader036/viewer/2022062420/62ae9ebb6645441612563524/html5/thumbnails/77.jpg)
Expression AddressComputa9on Address
0x8(%rdx)
(%rdx,%rcx)
(%rdx,%rcx,4)
0x80(,%rdx,2)
CarnegieMellon
AddressComputa.onExamples
Expression AddressComputa9on Address
0x8(%rdx)
(%rdx,%rcx)
(%rdx,%rcx,4)
0x80(,%rdx,2)
%rdx 0xf000
%rcx 0x100
![Page 78: Machine Program: Basics](https://reader036.vdocuments.site/reader036/viewer/2022062420/62ae9ebb6645441612563524/html5/thumbnails/78.jpg)
Expression AddressComputa9on Address
0x8(%rdx)
(%rdx,%rcx)
(%rdx,%rcx,4)
0x80(,%rdx,2)
CarnegieMellon
AddressComputa.onExamples
Expression AddressComputa9on Address
0x8(%rdx) 0xf000 + 0x8 0xf008
(%rdx,%rcx)
(%rdx,%rcx,4)
0x80(,%rdx,2)
%rdx 0xf000
%rcx 0x100
![Page 79: Machine Program: Basics](https://reader036.vdocuments.site/reader036/viewer/2022062420/62ae9ebb6645441612563524/html5/thumbnails/79.jpg)
Expression AddressComputa9on Address
0x8(%rdx)
(%rdx,%rcx)
(%rdx,%rcx,4)
0x80(,%rdx,2)
CarnegieMellon
AddressComputa.onExamples
Expression AddressComputa9on Address
0x8(%rdx) 0xf000 + 0x8 0xf008
(%rdx,%rcx) 0xf000 + 0x100 0xf100
(%rdx,%rcx,4)
0x80(,%rdx,2)
%rdx 0xf000
%rcx 0x100
![Page 80: Machine Program: Basics](https://reader036.vdocuments.site/reader036/viewer/2022062420/62ae9ebb6645441612563524/html5/thumbnails/80.jpg)
Expression AddressComputa9on Address
0x8(%rdx)
(%rdx,%rcx)
(%rdx,%rcx,4)
0x80(,%rdx,2)
CarnegieMellon
AddressComputa.onExamples
Expression AddressComputa9on Address
0x8(%rdx) 0xf000 + 0x8 0xf008
(%rdx,%rcx) 0xf000 + 0x100 0xf100
(%rdx,%rcx,4) 0xf000 + 4*0x100 0xf400
0x80(,%rdx,2)
%rdx 0xf000
%rcx 0x100
![Page 81: Machine Program: Basics](https://reader036.vdocuments.site/reader036/viewer/2022062420/62ae9ebb6645441612563524/html5/thumbnails/81.jpg)
Expression AddressComputa9on Address
0x8(%rdx)
(%rdx,%rcx)
(%rdx,%rcx,4)
0x80(,%rdx,2)
CarnegieMellon
AddressComputa.onExamples
Expression AddressComputa9on Address
0x8(%rdx) 0xf000 + 0x8 0xf008
(%rdx,%rcx) 0xf000 + 0x100 0xf100
(%rdx,%rcx,4) 0xf000 + 4*0x100 0xf400
0x80(,%rdx,2) 2*0xf000 + 0x80 0x1e080
%rdx 0xf000
%rcx 0x100
![Page 82: Machine Program: Basics](https://reader036.vdocuments.site/reader036/viewer/2022062420/62ae9ebb6645441612563524/html5/thumbnails/82.jpg)
Example
Memory
movq $1, 8(%rdi, %rax, 8)
movq $2, (%rdi, %rbx, 8)
a[3]
a[2]
a[1]
a[0]
…… …
a[4]
CPU
0x00…0050 PC:
IR: movq $1, 8(%rdi, %rax, 8)
RAX: 0x00…0001
0x00…0002
0x00…0010
…
RBX:
RCX:
RDX:
RSI:
RDI:
RSP:
RBP:
PC
a[5]
a[6]
0x00…0058 0x00…0050
0x00…0010 0x00…0018 0x00…0020 0x00…0028 0x00…0030
0x00…0038 0x00…0040 0x00…0048
0x00…0060
![Page 83: Machine Program: Basics](https://reader036.vdocuments.site/reader036/viewer/2022062420/62ae9ebb6645441612563524/html5/thumbnails/83.jpg)
Example
Memory
movq $1, 8(%rdi, %rax, 8)
movq $2, (%rdi, %rbx, 8)
a[3]
a[2]: 1
a[1]
a[0]
…… …
a[4]
CPU
0x00…0050 PC:
IR: movq $1, 8(%rdi, %rax, 8)
RAX: 0x00…0001
0x00…0002
0x00…0010
…
RBX:
RCX:
RDX:
RSI:
RDI:
RSP:
RBP:
PC
a[5]
a[6]
0x00…0058 0x00…0050
0x00…0010 0x00…0018 0x00…0020 0x00…0028 0x00…0030
0x00…0038 0x00…0040 0x00…0048
0x00…0060
![Page 84: Machine Program: Basics](https://reader036.vdocuments.site/reader036/viewer/2022062420/62ae9ebb6645441612563524/html5/thumbnails/84.jpg)
Example
Memory
movq $1, 8(%rdi, %rax, 8)
movq $2, (%rdi, %rbx, 8)
a[3]
a[2]: 2
a[1]
a[0]
…… …
a[4]
CPU
0x00…0058 PC:
IR: movq $1, (%rdi, %rbx, 8)
RAX: 0x00…0001
0x00…0002
0x00…0010
…
RBX:
RCX:
RDX:
RSI:
RDI:
RSP:
RBP:
PC
a[5]
a[6]
0x00…0058 0x00…0050
0x00…0010 0x00…0018 0x00…0020 0x00…0028 0x00…0030
0x00…0038 0x00…0040 0x00…0048
0x00…0060
![Page 85: Machine Program: Basics](https://reader036.vdocuments.site/reader036/viewer/2022062420/62ae9ebb6645441612563524/html5/thumbnails/85.jpg)
mov{bwlq}
movb src, dest
Copy a byte from the source operand to the destination operand. e.g., movb %al, %bl
movw src, dest
Copy a word from the source operand to the destination operand. e.g., movw %ax, %bx
movl src, dest
Copy a long (32 bits) from the source operand to the destination operand. e.g., movl %eax, %ebx
movq src, dest
Copy a quadword from the source operand to the destination operand. e.g., movq %rax, %rbx
![Page 86: Machine Program: Basics](https://reader036.vdocuments.site/reader036/viewer/2022062420/62ae9ebb6645441612563524/html5/thumbnails/86.jpg)
How to initialized a register with a memory address?
leaq Source, Dest – load effective address: set Dest to the address
denoted by Source address mode expression
![Page 87: Machine Program: Basics](https://reader036.vdocuments.site/reader036/viewer/2022062420/62ae9ebb6645441612563524/html5/thumbnails/87.jpg)
Example
Memory
movq 8(%rdi), %rax
movq (%rbx), %rcx
leaq 8(%rdi), %rbx
300
200
100
…… …
CPU
0x00…0048 PC:
IR: leaq 8(%rdi),%rbx
RAX:
0x00…0010
…
RBX:
RCX:
RDX:
RSI:
RDI:
RSP:
RBP:
PC
0x00…0058 0x00…0050
0x00…0010 0x00…0018 0x00…0020 0x00…0028 0x00…0030
0x00…0038 0x00…0040 0x00…0048
0x00…0060
![Page 88: Machine Program: Basics](https://reader036.vdocuments.site/reader036/viewer/2022062420/62ae9ebb6645441612563524/html5/thumbnails/88.jpg)
Example
Memory
movq 8(%rdi), %rax
movq (%rbx), %rcx
leaq 8(%rdi), %rbx
300
200
100
…… …
CPU
0x00…0048 PC:
IR: leaq 8(%rdi),%rbx
RAX:
0x00…0018
0x00…0010
…
RBX:
RCX:
RDX:
RSI:
RDI:
RSP:
RBP:
PC
0x00…0058 0x00…0050
0x00…0010 0x00…0018 0x00…0020 0x00…0028 0x00…0030
0x00…0038 0x00…0040 0x00…0048
0x00…0060
![Page 89: Machine Program: Basics](https://reader036.vdocuments.site/reader036/viewer/2022062420/62ae9ebb6645441612563524/html5/thumbnails/89.jpg)
Example
Memory
movq 8(%rdi), %rax
movq (%rbx), %rcx
leaq 8(%rdi), %rbx
300
200
100
…… …
CPU
0x00…0050 PC:
IR: movq 8(%rdi),%rax
RAX:
0x00…0018
0x00…0010
…
RBX:
RCX:
RDX:
RSI:
RDI:
RSP:
RBP:
PC
0x00…0058 0x00…0050
0x00…0010 0x00…0018 0x00…0020 0x00…0028 0x00…0030
0x00…0038 0x00…0040 0x00…0048
0x00…0060
![Page 90: Machine Program: Basics](https://reader036.vdocuments.site/reader036/viewer/2022062420/62ae9ebb6645441612563524/html5/thumbnails/90.jpg)
Example
Memory
movq 8(%rdi), %rax
movq (%rbx), %rcx
leaq 8(%rdi), %rbx
300
200
100
…… …
CPU
0x00…0050 PC:
IR: movq 8(%rdi),%rax
RAX: 200
0x00…0018
0x00…0010
…
RBX:
RCX:
RDX:
RSI:
RDI:
RSP:
RBP:
PC
0x00…0058 0x00…0050
0x00…0010 0x00…0018 0x00…0020 0x00…0028 0x00…0030
0x00…0038 0x00…0040 0x00…0048
0x00…0060
![Page 91: Machine Program: Basics](https://reader036.vdocuments.site/reader036/viewer/2022062420/62ae9ebb6645441612563524/html5/thumbnails/91.jpg)
Example
Memory
movq 8(%rdi), %rax
movq (%rbx), %rcx
leaq 8(%rdi), %rbx
300
200
100
…… …
CPU
0x00…0058 PC:
IR: movq (%rbx),%rcx
RAX: 200
0x00…0018
0x00…0010
…
RBX:
RCX:
RDX:
RSI:
RDI:
RSP:
RBP:
PC 0x00…0058 0x00…0050
0x00…0010 0x00…0018 0x00…0020 0x00…0028 0x00…0030
0x00…0038 0x00…0040 0x00…0048
0x00…0060
![Page 92: Machine Program: Basics](https://reader036.vdocuments.site/reader036/viewer/2022062420/62ae9ebb6645441612563524/html5/thumbnails/92.jpg)
Example
Memory
movq 8(%rdi), %rax
movq (%rbx), %rcx
leaq 8(%rdi), %rbx
300
200
100
…… …
CPU
0x00…0058 PC:
IR: movq (%rbx),%rcx
RAX: 200
0x00…0018
200
0x00…0010
…
RBX:
RCX:
RDX:
RSI:
RDI:
RSP:
RBP:
PC 0x00…0058 0x00…0050
0x00…0010 0x00…0018 0x00…0020 0x00…0028 0x00…0030
0x00…0038 0x00…0040 0x00…0048
0x00…0060
![Page 93: Machine Program: Basics](https://reader036.vdocuments.site/reader036/viewer/2022062420/62ae9ebb6645441612563524/html5/thumbnails/93.jpg)
Other usage of leaq
Computing arithmetic expressions of the form x + k*y + d (k = 1, 2, 4, or 8)
longm3(longx){returnx*3;}
Assume %rdi has the value of x
![Page 94: Machine Program: Basics](https://reader036.vdocuments.site/reader036/viewer/2022062420/62ae9ebb6645441612563524/html5/thumbnails/94.jpg)
Other usage of leaq
Computing arithmetic expressions of the form x + k*y + d (k = 1, 2, 4, or 8)
longm3(longx){returnx*3;}
leaq(%rdi,%rdi,2),%rax
Assume %rdi has the value of x
![Page 95: Machine Program: Basics](https://reader036.vdocuments.site/reader036/viewer/2022062420/62ae9ebb6645441612563524/html5/thumbnails/95.jpg)
CarnegieMellon
Arithmetic Expression Puzzle
leaq (%rdi,%rsi,2),%raxleaq (%rax,%rax,4),%rax
Suppose %rdi, %rsi, %rax contains variable x, y, s respsectively
longf(longx,longy){
longs=??;returns;}
![Page 96: Machine Program: Basics](https://reader036.vdocuments.site/reader036/viewer/2022062420/62ae9ebb6645441612563524/html5/thumbnails/96.jpg)
CarnegieMellon
Arithmetic Expression Puzzle
leaq (%rdi,%rsi,2),%raxleaq (%rax,%rax,4),%rax
Suppose %rdi, %rsi, %rax contains variable x, y, s respsectively
longf(longx,longy){
longs=5(x+2y);returns;}
![Page 97: Machine Program: Basics](https://reader036.vdocuments.site/reader036/viewer/2022062420/62ae9ebb6645441612563524/html5/thumbnails/97.jpg)
CarnegieMellon
Some Arithmetic Operations Two Operand Instructions:
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
![Page 98: Machine Program: Basics](https://reader036.vdocuments.site/reader036/viewer/2022062420/62ae9ebb6645441612563524/html5/thumbnails/98.jpg)
CarnegieMellon
Some Arithmetic Operations
One Operand Instructions incq Dest Dest = Dest + 1 decq Dest Dest = Dest - 1 negq Dest Dest = - Dest notq Dest Dest = ~Dest
![Page 99: Machine Program: Basics](https://reader036.vdocuments.site/reader036/viewer/2022062420/62ae9ebb6645441612563524/html5/thumbnails/99.jpg)
Example
Memory
addq %rax , 8(%rdi)
300
200
100
…… …
CPU
0x00…0058 PC:
IR: addq %rax, 8(%rdi)
RAX: 0x00…0001
0x00…0010
…
RBX:
RCX:
RDX:
RSI:
RDI:
RSP:
RBP:
PC 0x00…0058 0x00…0050
0x00…0010 0x00…0018 0x00…0020 0x00…0028 0x00…0030
0x00…0038 0x00…0040 0x00…0048
0x00…0060
![Page 100: Machine Program: Basics](https://reader036.vdocuments.site/reader036/viewer/2022062420/62ae9ebb6645441612563524/html5/thumbnails/100.jpg)
Example
Memory
addq %rax , 8(%rdi)
300
201
100
…… …
CPU
0x00…0058 PC:
IR: addq %rax, 8(%rdi)
RAX: 0x00…0001
0x00…0010
…
RBX:
RCX:
RDX:
RSI:
RDI:
RSP:
RBP:
PC 0x00…0058 0x00…0050
0x00…0010 0x00…0018 0x00…0020 0x00…0028 0x00…0030
0x00…0038 0x00…0040 0x00…0048
0x00…0060
![Page 101: Machine Program: Basics](https://reader036.vdocuments.site/reader036/viewer/2022062420/62ae9ebb6645441612563524/html5/thumbnails/101.jpg)
CarnegieMellon
Some Arithmetic Operations Two Operand Instructions:
add{bwlq} Src,Dest Dest = Dest + Src sub{bwlq} Src,Dest Dest = Dest - Src imul{bwlq} Src,Dest Dest = Dest * Src sal{bwlq} Src,Dest Dest = Dest << Src Also called shlq sar{bwlq} Src,Dest Dest = Dest >> Src Arithmetic shr{bwlq} Src,Dest Dest = Dest >> Src Logical xor{bwlq} Src,Dest Dest = Dest ^ Src and{bwlq} Src,Dest Dest = Dest & Src or{bwlq} Src,Dest Dest = Dest | Src
![Page 102: Machine Program: Basics](https://reader036.vdocuments.site/reader036/viewer/2022062420/62ae9ebb6645441612563524/html5/thumbnails/102.jpg)
CarnegieMellon
Some Arithmetic Operations
One Operand Instructions inc{bwlq} Dest Dest = Dest + 1 dec{bwlq} Dest Dest = Dest - 1 neg{bwlq} Dest Dest = - Dest not{bwlq} Dest Dest = ~Dest
![Page 103: Machine Program: Basics](https://reader036.vdocuments.site/reader036/viewer/2022062420/62ae9ebb6645441612563524/html5/thumbnails/103.jpg)
Good news
Lab2: 10/15 night