Lecture 5: Introduction to Assembly€¦ · x86-64 Assembly Language 5 •Evolutionary design,...
Transcript of Lecture 5: Introduction to Assembly€¦ · x86-64 Assembly Language 5 •Evolutionary design,...
![Page 1: Lecture 5: Introduction to Assembly€¦ · x86-64 Assembly Language 5 •Evolutionary design, going back to 8086 in 1978 •Basis for original IBM Personal Computer, 16-bits •Intel](https://reader034.fdocuments.net/reader034/viewer/2022052007/601b05313a062e33cb77799a/html5/thumbnails/1.jpg)
CS 105 February 5, 2020
Lecture 5: Introduction to Assembly
![Page 2: Lecture 5: Introduction to Assembly€¦ · x86-64 Assembly Language 5 •Evolutionary design, going back to 8086 in 1978 •Basis for original IBM Personal Computer, 16-bits •Intel](https://reader034.fdocuments.net/reader034/viewer/2022052007/601b05313a062e33cb77799a/html5/thumbnails/2.jpg)
#include<stdio.h>
int main(int argc, char ** argv){
printf("Hello world!\n");
return 0;}
Programs55 48 89 e548 83 ec 20 48 8d 05 25 00 00 00 c7 45 fc 00 00 00 00 89 7d f8 48 89 75 f0 48 89 c7b0 00e8 00 00 00 0031 c989 45 ec89 c848 83 c4 20 5dc3
![Page 3: Lecture 5: Introduction to Assembly€¦ · x86-64 Assembly Language 5 •Evolutionary design, going back to 8086 in 1978 •Basis for original IBM Personal Computer, 16-bits •Intel](https://reader034.fdocuments.net/reader034/viewer/2022052007/601b05313a062e33cb77799a/html5/thumbnails/3.jpg)
Compilation
Pre-processor(cpp)
hello.i Compiler(cc1)
hello.s Assembler(as)
hello.o Linker(ld)
hellohello.c
Sourceprogram(text)
Modifiedsourceprogram(text)
Assemblyprogram(text)
Relocatableobjectprograms(binary)
Executableobjectprogram(binary)
printf.o
#include<stdio.h>
int main(int argc, char ** argv){
printf("Hello world!\n");
return 0;}
…int printf(const char *
restrict, ...)
__attribute__((__format__ (__printf__, 1, 2)));…int main(int argc,
char ** argv){
printf("Hello world!\n");
return 0;}
pushq %rbpmovq %rsp, %rbpsubq $32, %rspleaq L_.str(%rip), %raxmovl $0, -4(%rbp)movl %edi, -8(%rbp)movq %rsi, -16(%rbp)movq %rax, %rdimovb $0, %alcallq _printfxorl %ecx, %ecxmovl %eax, -20(%rbp) movl %ecx, %eaxaddq $32, %rsppopq %rbpretq
55 48 89 e548 83 ec 20 48 8d 05 25 00 00 00 c7 45 fc 00 00 00 00 89 7d f8 48 89 75 f0 48 89 c7b0 00e8 00 00 00 0031 c989 45 ec89 c848 83 c4 20 5dc3
![Page 4: Lecture 5: Introduction to Assembly€¦ · x86-64 Assembly Language 5 •Evolutionary design, going back to 8086 in 1978 •Basis for original IBM Personal Computer, 16-bits •Intel](https://reader034.fdocuments.net/reader034/viewer/2022052007/601b05313a062e33cb77799a/html5/thumbnails/4.jpg)
x86-64 Assembly Language
5
• Evolutionary design, going back to 8086 in 1978• Basis for original IBM Personal Computer, 16-bits
• Intel Pentium 4E (2004): 64 bit instruction set
• High-level languages are translated into x86 instructions and then executed on the CPU• Actual instructions are sequences of bytes• We give them mnemonic names
![Page 5: Lecture 5: Introduction to Assembly€¦ · x86-64 Assembly Language 5 •Evolutionary design, going back to 8086 in 1978 •Basis for original IBM Personal Computer, 16-bits •Intel](https://reader034.fdocuments.net/reader034/viewer/2022052007/601b05313a062e33cb77799a/html5/thumbnails/5.jpg)
Memory0x7FFF
0x0000
Central Processing Unit (CPU)
Assembly/Machine Code View
6
Programmer-Visible State} PC: Program counter (%rip)} Register file: 16 Registers} Float registers} Condition codes
Memory} Byte addressable array} Code and user data} Stack to support
procedures
PC
Data
Addresses
InstructionsCodeData
Stack
Heap
Registers
ConditionCodes
Float registers
![Page 6: Lecture 5: Introduction to Assembly€¦ · x86-64 Assembly Language 5 •Evolutionary design, going back to 8086 in 1978 •Basis for original IBM Personal Computer, 16-bits •Intel](https://reader034.fdocuments.net/reader034/viewer/2022052007/601b05313a062e33cb77799a/html5/thumbnails/6.jpg)
Program Counter• Stores the address of the next instruction to execute
• Repeat forever:• Fetch instruction at address in PC• Execute the instruction• Update PC
![Page 7: Lecture 5: Introduction to Assembly€¦ · x86-64 Assembly Language 5 •Evolutionary design, going back to 8086 in 1978 •Basis for original IBM Personal Computer, 16-bits •Intel](https://reader034.fdocuments.net/reader034/viewer/2022052007/601b05313a062e33cb77799a/html5/thumbnails/7.jpg)
Assembly Characteristics: Operations
8
• Transfer data between memory and register• Load data from memory into register• Store register data into memory
• Perform arithmetic function on register or memory data
• Transfer control• Conditional branches• Unconditional jumps to/from procedures
![Page 8: Lecture 5: Introduction to Assembly€¦ · x86-64 Assembly Language 5 •Evolutionary design, going back to 8086 in 1978 •Basis for original IBM Personal Computer, 16-bits •Intel](https://reader034.fdocuments.net/reader034/viewer/2022052007/601b05313a062e33cb77799a/html5/thumbnails/8.jpg)
DATA TRANSFER IN ASSEMBLY
![Page 9: Lecture 5: Introduction to Assembly€¦ · x86-64 Assembly Language 5 •Evolutionary design, going back to 8086 in 1978 •Basis for original IBM Personal Computer, 16-bits •Intel](https://reader034.fdocuments.net/reader034/viewer/2022052007/601b05313a062e33cb77799a/html5/thumbnails/9.jpg)
X86-64 Integer Registers
10
%rsp
%r8
%r9
%r10
%r11
%r12
%r13
%r14
%r15
%rax
%rbx
%rcx
%rdx
%rsi
%rdi
%rbp
(second argument)
(first argument)
(stack pointer)
(base pointer)
(third argument)
(fourth argument)
(function result)
![Page 10: Lecture 5: Introduction to Assembly€¦ · x86-64 Assembly Language 5 •Evolutionary design, going back to 8086 in 1978 •Basis for original IBM Personal Computer, 16-bits •Intel](https://reader034.fdocuments.net/reader034/viewer/2022052007/601b05313a062e33cb77799a/html5/thumbnails/10.jpg)
Data Movement Instructions• MOV source, dest Moves data source->dest
![Page 11: Lecture 5: Introduction to Assembly€¦ · x86-64 Assembly Language 5 •Evolutionary design, going back to 8086 in 1978 •Basis for original IBM Personal Computer, 16-bits •Intel](https://reader034.fdocuments.net/reader034/viewer/2022052007/601b05313a062e33cb77799a/html5/thumbnails/11.jpg)
Exercise
• What are the values of the following operands (assuming register and memory state shown above)?1. %rax2. 0x1043. $0x1084. (%rax)5. 4(%rax)
Register Value%rax 0x100%rcx 0x1%rdx 0x3
Memory Address Value0x100 0xFF0x104 0xAB0x108 0x13
![Page 12: Lecture 5: Introduction to Assembly€¦ · x86-64 Assembly Language 5 •Evolutionary design, going back to 8086 in 1978 •Basis for original IBM Personal Computer, 16-bits •Intel](https://reader034.fdocuments.net/reader034/viewer/2022052007/601b05313a062e33cb77799a/html5/thumbnails/12.jpg)
Sizes of C Data Types in x86-64
C declaration Size (bytes) Intel data type Assembly suffixchar 1 Byte bshort 2 Word wint 4 Double word llong 8 Quad word qchar * 8 Quad word qfloat 4 Single precision sdouble 8 Double precision l
![Page 13: Lecture 5: Introduction to Assembly€¦ · x86-64 Assembly Language 5 •Evolutionary design, going back to 8086 in 1978 •Basis for original IBM Personal Computer, 16-bits •Intel](https://reader034.fdocuments.net/reader034/viewer/2022052007/601b05313a062e33cb77799a/html5/thumbnails/13.jpg)
Data Movement Instructions• MOV source, dest Move data source->dest
• movb Move byte• movw Move word• movl Move double word• movq Move quad word
![Page 14: Lecture 5: Introduction to Assembly€¦ · x86-64 Assembly Language 5 •Evolutionary design, going back to 8086 in 1978 •Basis for original IBM Personal Computer, 16-bits •Intel](https://reader034.fdocuments.net/reader034/viewer/2022052007/601b05313a062e33cb77799a/html5/thumbnails/14.jpg)
X86-64 Integer Registers
16
%rsp
%eax
%ebx
%ecx
%edx
%esi
%edi
%esp
%ebp
%r8d
%r9d
%r10d
%r11d
%r12d
%r13d
%r14d
%r15d
%r8
%r9
%r10
%r11
%r12
%r13
%r14
%r15
%rax
%rbx
%rcx
%rdx
%rsi
%rdi
%rbp
%ax %al
%bx %bl
%cx %cl
%dx %dl
%si %sil
%di %dil
%sp %bsl
%bp %bpl
![Page 15: Lecture 5: Introduction to Assembly€¦ · x86-64 Assembly Language 5 •Evolutionary design, going back to 8086 in 1978 •Basis for original IBM Personal Computer, 16-bits •Intel](https://reader034.fdocuments.net/reader034/viewer/2022052007/601b05313a062e33cb77799a/html5/thumbnails/15.jpg)
movq Operand Combinations
Cannot do memory-memory transfer with a single instruction
movq
Imm
Reg
Mem
RegMem
RegMem
Reg
Source Dest C Analog
movq $0x4,%rax temp = 0x4;
movq $-147,(%rax) *p = -147;
movq %rax,%rdx temp2 = temp1;
movq %rax,(%rdx) *p = temp;
movq (%rax),%rdx temp = *p;
Src,Dest
![Page 16: Lecture 5: Introduction to Assembly€¦ · x86-64 Assembly Language 5 •Evolutionary design, going back to 8086 in 1978 •Basis for original IBM Personal Computer, 16-bits •Intel](https://reader034.fdocuments.net/reader034/viewer/2022052007/601b05313a062e33cb77799a/html5/thumbnails/16.jpg)
Exercise• Write a C function void decode1(long *xp, long *yp, long *zp)
that will do the same thing as the following assembly code:
decode1:movq (%rdi), %r8movq (%rsi), %rcxmovq (%rdx), %raxmovq %r8, (%rsi)movq %rcx, (%rdx)movq %rax, (%rdi)ret
void decode1(long *xp, long *yp, long *zp){
}
Register Use(s)
%rdi Argument xp
%rsi Argument yp
%rdx Argument zp
![Page 17: Lecture 5: Introduction to Assembly€¦ · x86-64 Assembly Language 5 •Evolutionary design, going back to 8086 in 1978 •Basis for original IBM Personal Computer, 16-bits •Intel](https://reader034.fdocuments.net/reader034/viewer/2022052007/601b05313a062e33cb77799a/html5/thumbnails/17.jpg)
C is close to Machine Language• C Code
• Store value t where designated by dest
• Assembly• Move 8-byte value to
memory• Quad words in x86-64 parlance
• Operands:t: Register %raxdest: Register %rbx*dest: Memory M[%rbx]
• Object Code• 3-byte instruction• at address 0x40059e
19
*dest = t;
movq %rax, (%rbx)
0x40059e: 48 89 03