1 / 18

Instructor: John Barr

Machine-Level Programming II: Basics Comp 21000 : Introduction to Computer Organization & Systems Spring 2016. Instructor: John Barr * Modified slides from the book “Computer Systems: a Programmer’s Perspective”, Randy Bryant & David O’Hallaron , 2015. Machine Programming I: Basics.

edwardsd
Download Presentation

Instructor: John Barr

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Machine-Level Programming II: BasicsComp 21000: Introduction to Computer Organization & Systems Spring 2016 Instructor: John Barr * Modified slides from the book “Computer Systems: a Programmer’s Perspective”, Randy Bryant & David O’Hallaron, 2015

  2. Machine Programming I: Basics • History of Intel processors and architectures • C, assembly, machine code • Assembly Basics: Registers, operands, move • Intro to x86-64

  3. Special memory areas: stack and heap Kernel virtual memory • Stack • Space in Memory allocated to each running program (called a process) • Used to store “temporary” variable values • Used to store variables for functions • Heap • Space in Memory allocated to each running program (process) • Used to store dynamically allocated variables User stack (created at runtime) Memory mapped region for shared libraries Run-time heap (created at runtime by malloc) Read/write segment (.data, .bss) Read-only segment (.init, .text, .rodata) Unused

  4. Example of Simple Addressing Modes void swap (long *xp, long *yp) { long t0 = *xp; long t1 = *yp; *xp = t1; *yp = t0; } swap: movq (%rdi), %rax movq (%rsi), %rdx movq %rdx, (%rdi) movq %rax, (%rsi) ret Note: if you look at the assembly code, it’s much more complicated; we’re ignoring some code to simplify.

  5. Understanding Swap() Memory Registers void swap (long *xp, long *yp) { long t0 = *xp; long t1 = *yp; *xp = t1; *yp = t0; } %rdi %rsi %rax %rdx Register Value %rdixp %rsiyp %rax t0 %rdx t1 swap: movq (%rdi), %rax # t0 = *xp movq (%rsi), %rdx # t1 = *yp movq %rdx, (%rdi) # *xp = t1 movq %rax, (%rsi) # *yp = t0 ret

  6. Understanding Swap() Memory Registers Address 0x120 123 %rdi 0x120 0x118 %rsi 0x100 0x110 %rax 0x108 %rdx 456 0x100 swap: movq (%rdi), %rax # t0 = *xp movq (%rsi), %rdx # t1 = *yp movq %rdx, (%rdi) # *xp = t1 movq %rax, (%rsi) # *yp = t0 ret

  7. Understanding Swap() Memory Registers Address 0x120 123 %rdi 0x120 0x118 %rsi 0x100 0x110 %rax 123 0x108 %rdx 456 0x100 swap: movq (%rdi), %rax # t0 = *xp movq (%rsi), %rdx # t1 = *yp movq %rdx, (%rdi) # *xp = t1 movq %rax, (%rsi) # *yp = t0 ret

  8. Understanding Swap() Memory Registers Address 0x120 123 %rdi 0x120 0x118 %rsi 0x100 0x110 %rax 123 0x108 %rdx 456 456 0x100 swap: movq (%rdi), %rax # t0 = *xp movq (%rsi), %rdx # t1 = *yp movq %rdx, (%rdi) # *xp = t1 movq %rax, (%rsi) # *yp = t0 ret

  9. Understanding Swap() Memory Registers Address 0x120 456 %rdi 0x120 0x118 %rsi 0x100 0x110 %rax 123 0x108 %rdx 456 456 0x100 swap: movq (%rdi), %rax # t0 = *xp movq (%rsi), %rdx # t1 = *yp movq %rdx, (%rdi) # *xp = t1 movq %rax, (%rsi) # *yp = t0 ret

  10. Understanding Swap() Memory Registers Address 0x120 456 %rdi 0x120 0x118 %rsi 0x100 0x110 %rax 123 0x108 %rdx 456 123 0x100 swap: movq (%rdi), %rax # t0 = *xp movq (%rsi), %rdx # t1 = *yp movq %rdx, (%rdi) # *xp = t1 movq %rax, (%rsi) # *yp = t0 ret

  11. Complete Memory Addressing Modes • Most General Form D(Rb,Ri,S) Mem[Reg[Rb]+S*Reg[Ri]+ D] • D: Constant “displacement” 1, 2, or 4 bytes • Rb: Base register: Any of 16 integer registers • Ri: Index register: Any, except for %rsp • Unlikely you’d use %rbp, either • S: Scale: 1, 2, 4, or 8 (why these numbers?) • Special Cases (Rb,Ri) Mem[Reg[Rb]+Reg[Ri]] D(Rb,Ri) Mem[Reg[Rb]+Reg[Ri]+D] (Rb,Ri,S) Mem[Reg[Rb]+S*Reg[Ri]] D(Rb,Ri,S) Mem[Reg[Rb]+S*Reg[Ri]+D]

  12. Address Computation Examples %rdx 0xf000 %rcx 0x100

  13. Address Computation Examples %edx 0xf000 %ecx 0x100 0xf000+ 0x8 0xf008 0xf000 + 0x100 0xf100 0xf000 + 4*0x100 0xf400 2*0xf000 + 0x80 0x1e080

  14. Address Computation Instruction lea = load effective address • leaqSrc,Dest • Src is address mode expression • Set Dest to address denoted by expression • Format • Looks like a memory access • Does not actually access memory • Rather, calculates the memory address, then stores in a register • Uses • Computing addresses without a memory reference • E.g., translation of p = &x[i]; • Computing arithmetic expressions of the form x + k*y • k = 1, 2, 4, or 8.

  15. Example Converted to ASM by compiler: long m12(long x) { return x*12; } leaq (%rdi,%rdi,2), %rax ;t <- x+x*2 salq $2, %rax ;return t<<2 ; or t * 4

  16. Address Computation Instruction Assume %rax holds value x and %rcx holds value y * Note the leading comma in the next to last entry

  17. Address Computation Instruction • %rdx x + 6 • %rdx x + y • %rdxx + (y * 4) • %rdxx + (x * 8) + 7 = (x * 9) + 7 • %rdx (x * 4) + 10 • %rdxx + (y * 2) + 9 Assume %rax holds value x and %rcx holds value y * Note the leading comma in the next to last entry

  18. Machine Programming II: Summary • History of Intel processors and architectures • Evolutionary design leads to many quirks and artifacts • C, assembly, machine code • New forms of visible state: program counter, registers,… • Compiler must transform statements, expressions, procedures into low-level instruction sequences • Assembly Basics: Registers, operands, move • The x86-64 move instructions cover wide range of data movement forms • Intro to x86-64 • A major departure from the style of code seen in IA32

More Related