Brief Assembly Refreshercr4bd/3330/S2018/... · 1/23/2018 · lasttime...
Transcript of Brief Assembly Refreshercr4bd/3330/S2018/... · 1/23/2018 · lasttime...
![Page 1: Brief Assembly Refreshercr4bd/3330/S2018/... · 1/23/2018 · lasttime processors↔memory,I/Odevices processor:sendaddresses(+values,sometimes) memory:replywithvalues(orstorevalue)](https://reader035.fdocuments.us/reader035/viewer/2022071405/60fa543e9dbbf135bd73dc1a/html5/thumbnails/1.jpg)
Brief Assembly Refresher
1
![Page 2: Brief Assembly Refreshercr4bd/3330/S2018/... · 1/23/2018 · lasttime processors↔memory,I/Odevices processor:sendaddresses(+values,sometimes) memory:replywithvalues(orstorevalue)](https://reader035.fdocuments.us/reader035/viewer/2022071405/60fa543e9dbbf135bd73dc1a/html5/thumbnails/2.jpg)
Changelog
Changes made in this version not seen in first lecture:23 Jan 2018: if-to-assembly if (...) goto needed b < 4223 Jan 2018: caller/callee-saved: correct comment about which registeris callee-saved23 Jan 2018: AT&T syntax: addresses: two more examples; correct100+ on last new one
1
![Page 3: Brief Assembly Refreshercr4bd/3330/S2018/... · 1/23/2018 · lasttime processors↔memory,I/Odevices processor:sendaddresses(+values,sometimes) memory:replywithvalues(orstorevalue)](https://reader035.fdocuments.us/reader035/viewer/2022071405/60fa543e9dbbf135bd73dc1a/html5/thumbnails/3.jpg)
last time
processors ↔ memory, I/O devicesprocessor: send addresses (+ values, sometimes)memory: reply with values (or store value)some addresses correspond to I/O devicestype of value read: why did processor read it?
endianness: little = least address is least significantlittle endian: 0x1234: 0x34 at address x + 0big endian: 0x1234: 0x12 at address x + 0
object files and linkingrelocations: “fill in the blank” with final addressessymbol table: location of labels within filememory addresses not decided until final executable
2
![Page 4: Brief Assembly Refreshercr4bd/3330/S2018/... · 1/23/2018 · lasttime processors↔memory,I/Odevices processor:sendaddresses(+values,sometimes) memory:replywithvalues(orstorevalue)](https://reader035.fdocuments.us/reader035/viewer/2022071405/60fa543e9dbbf135bd73dc1a/html5/thumbnails/4.jpg)
a logistics note
on the waitlist? there are labs/lectures open
if you can’t do the 2pm lecture, we can talk…
3
![Page 5: Brief Assembly Refreshercr4bd/3330/S2018/... · 1/23/2018 · lasttime processors↔memory,I/Odevices processor:sendaddresses(+values,sometimes) memory:replywithvalues(orstorevalue)](https://reader035.fdocuments.us/reader035/viewer/2022071405/60fa543e9dbbf135bd73dc1a/html5/thumbnails/5.jpg)
anonymous feedback
on Collab, I appreciate it — especially specific feedback:
“The way that topics were introduced in class today was really confusing for me.When Prof. Reiss discussed the I/O bridge, he didn’t first explain what each elementwas, making his explanation of the whole system extremely difficult to understand forthose of us who didn’t know what he was talking about. The explanation ofEndianness also was confusing, and really would’ve been helped along by referencinga decimal number that makes more sense to the students rather than a hex number,which most people aren’t nearly as comfortable with. Please consider changing yourstyle of explanation to make the concepts more clear.”
Hm — 2150 doesn’t cover processors the way I thought…
4
![Page 6: Brief Assembly Refreshercr4bd/3330/S2018/... · 1/23/2018 · lasttime processors↔memory,I/Odevices processor:sendaddresses(+values,sometimes) memory:replywithvalues(orstorevalue)](https://reader035.fdocuments.us/reader035/viewer/2022071405/60fa543e9dbbf135bd73dc1a/html5/thumbnails/6.jpg)
compilation pipelinemain.c
(C code)
compile
main.s(assembly)
assemblemain.o
(object file)(machine code)
linkingmain.exe
(executable)(machine code)
main.c:#include <stdio.h>int main(void) {
printf("Hello, World!\n");}
printf.o(object file)
5
![Page 7: Brief Assembly Refreshercr4bd/3330/S2018/... · 1/23/2018 · lasttime processors↔memory,I/Odevices processor:sendaddresses(+values,sometimes) memory:replywithvalues(orstorevalue)](https://reader035.fdocuments.us/reader035/viewer/2022071405/60fa543e9dbbf135bd73dc1a/html5/thumbnails/7.jpg)
compilation pipelinemain.c
(C code)
compile
main.s(assembly)
assemblemain.o
(object file)(machine code)
linkingmain.exe
(executable)(machine code)
main.c:#include <stdio.h>int main(void) {
printf("Hello, World!\n");}
printf.o(object file)
5
![Page 8: Brief Assembly Refreshercr4bd/3330/S2018/... · 1/23/2018 · lasttime processors↔memory,I/Odevices processor:sendaddresses(+values,sometimes) memory:replywithvalues(orstorevalue)](https://reader035.fdocuments.us/reader035/viewer/2022071405/60fa543e9dbbf135bd73dc1a/html5/thumbnails/8.jpg)
compilation pipelinemain.c
(C code)
compile
main.s(assembly)
assemblemain.o
(object file)(machine code)
linkingmain.exe
(executable)(machine code)
main.c:#include <stdio.h>int main(void) {
printf("Hello, World!\n");}
printf.o(object file)
5
![Page 9: Brief Assembly Refreshercr4bd/3330/S2018/... · 1/23/2018 · lasttime processors↔memory,I/Odevices processor:sendaddresses(+values,sometimes) memory:replywithvalues(orstorevalue)](https://reader035.fdocuments.us/reader035/viewer/2022071405/60fa543e9dbbf135bd73dc1a/html5/thumbnails/9.jpg)
compilation commandscompile: gcc -S file.c ⇒ file.s (assembly)assemble: gcc -c file.s ⇒ file.o (object file)link: gcc -o file file.o ⇒ file (executable)
c+a: gcc -c file.c ⇒ file.oc+a+l: gcc -o file file.c ⇒ file…
6
![Page 10: Brief Assembly Refreshercr4bd/3330/S2018/... · 1/23/2018 · lasttime processors↔memory,I/Odevices processor:sendaddresses(+values,sometimes) memory:replywithvalues(orstorevalue)](https://reader035.fdocuments.us/reader035/viewer/2022071405/60fa543e9dbbf135bd73dc1a/html5/thumbnails/10.jpg)
what’s in those files?#include <stdio.h>int main(void) {
puts("Hello, World!");return 0;
}
hello.c.text
main:sub $8, %rspmov $.Lstr, %rdicall putsxor %eax, %eaxadd $8, %rspret
.data.Lstr: .string "Hello,␣World!"
hello.s
text (code) segment:48 83 EC 08 BF 00 00 00 00 E8 00 0000 00 31 C0 48 83 C4 08 C3data segment:48 65 6C 6C 6F 2C 20 57 6F 72 6C 00relocations:
take 0s at and replace withtext, byte 6 ( ) data segment, byte 0text, byte 10 ( ) address of puts
symbol table:main text byte 0
hello.o
(actually binary, but shown as hexadecimal) …48 83 EC 08 BF A7 02 04 00E8 08 4A 04 00 31 C0 4883 C4 08 C3 ……(code from stdio.o) …48 65 6C 6C 6F 2C 20 57 6F72 6C 00 ……(data from stdio.o) …
hello.exe+ stdio.o
7
![Page 11: Brief Assembly Refreshercr4bd/3330/S2018/... · 1/23/2018 · lasttime processors↔memory,I/Odevices processor:sendaddresses(+values,sometimes) memory:replywithvalues(orstorevalue)](https://reader035.fdocuments.us/reader035/viewer/2022071405/60fa543e9dbbf135bd73dc1a/html5/thumbnails/11.jpg)
what’s in those files?#include <stdio.h>int main(void) {
puts("Hello, World!");return 0;
}
hello.c.text
main:sub $8, %rspmov $.Lstr, %rdicall putsxor %eax, %eaxadd $8, %rspret
.data.Lstr: .string "Hello,␣World!"
hello.s
text (code) segment:48 83 EC 08 BF 00 00 00 00 E8 00 0000 00 31 C0 48 83 C4 08 C3data segment:48 65 6C 6C 6F 2C 20 57 6F 72 6C 00relocations:
take 0s at and replace withtext, byte 6 ( ) data segment, byte 0text, byte 10 ( ) address of puts
symbol table:main text byte 0
hello.o
(actually binary, but shown as hexadecimal) …48 83 EC 08 BF A7 02 04 00E8 08 4A 04 00 31 C0 4883 C4 08 C3 ……(code from stdio.o) …48 65 6C 6C 6F 2C 20 57 6F72 6C 00 ……(data from stdio.o) …
hello.exe+ stdio.o
7
![Page 12: Brief Assembly Refreshercr4bd/3330/S2018/... · 1/23/2018 · lasttime processors↔memory,I/Odevices processor:sendaddresses(+values,sometimes) memory:replywithvalues(orstorevalue)](https://reader035.fdocuments.us/reader035/viewer/2022071405/60fa543e9dbbf135bd73dc1a/html5/thumbnails/12.jpg)
what’s in those files?#include <stdio.h>int main(void) {
puts("Hello, World!");return 0;
}
hello.c.text
main:sub $8, %rspmov $.Lstr, %rdicall putsxor %eax, %eaxadd $8, %rspret
.data.Lstr: .string "Hello,␣World!"
hello.s
text (code) segment:48 83 EC 08 BF 00 00 00 00 E8 00 0000 00 31 C0 48 83 C4 08 C3data segment:48 65 6C 6C 6F 2C 20 57 6F 72 6C 00relocations:
take 0s at and replace withtext, byte 6 ( ) data segment, byte 0text, byte 10 ( ) address of puts
symbol table:main text byte 0
hello.o
(actually binary, but shown as hexadecimal) …48 83 EC 08 BF A7 02 04 00E8 08 4A 04 00 31 C0 4883 C4 08 C3 ……(code from stdio.o) …48 65 6C 6C 6F 2C 20 57 6F72 6C 00 ……(data from stdio.o) …
hello.exe+ stdio.o
7
![Page 13: Brief Assembly Refreshercr4bd/3330/S2018/... · 1/23/2018 · lasttime processors↔memory,I/Odevices processor:sendaddresses(+values,sometimes) memory:replywithvalues(orstorevalue)](https://reader035.fdocuments.us/reader035/viewer/2022071405/60fa543e9dbbf135bd73dc1a/html5/thumbnails/13.jpg)
what’s in those files?#include <stdio.h>int main(void) {
puts("Hello, World!");return 0;
}
hello.c.text
main:sub $8, %rspmov $.Lstr, %rdicall putsxor %eax, %eaxadd $8, %rspret
.data.Lstr: .string "Hello,␣World!"
hello.s
text (code) segment:48 83 EC 08 BF 00 00 00 00 E8 00 0000 00 31 C0 48 83 C4 08 C3data segment:48 65 6C 6C 6F 2C 20 57 6F 72 6C 00relocations:
take 0s at and replace withtext, byte 6 ( ) data segment, byte 0text, byte 10 ( ) address of puts
symbol table:main text byte 0
hello.o
(actually binary, but shown as hexadecimal) …48 83 EC 08 BF A7 02 04 00E8 08 4A 04 00 31 C0 4883 C4 08 C3 ……(code from stdio.o) …48 65 6C 6C 6F 2C 20 57 6F72 6C 00 ……(data from stdio.o) …
hello.exe+ stdio.o
7
![Page 14: Brief Assembly Refreshercr4bd/3330/S2018/... · 1/23/2018 · lasttime processors↔memory,I/Odevices processor:sendaddresses(+values,sometimes) memory:replywithvalues(orstorevalue)](https://reader035.fdocuments.us/reader035/viewer/2022071405/60fa543e9dbbf135bd73dc1a/html5/thumbnails/14.jpg)
what’s in those files?#include <stdio.h>int main(void) {
puts("Hello, World!");return 0;
}
hello.c.text
main:sub $8, %rspmov $.Lstr, %rdicall putsxor %eax, %eaxadd $8, %rspret
.data.Lstr: .string "Hello,␣World!"
hello.s
text (code) segment:48 83 EC 08 BF 00 00 00 00 E8 00 0000 00 31 C0 48 83 C4 08 C3data segment:48 65 6C 6C 6F 2C 20 57 6F 72 6C 00relocations:
take 0s at and replace withtext, byte 6 ( ) data segment, byte 0text, byte 10 ( ) address of puts
symbol table:main text byte 0
hello.o
(actually binary, but shown as hexadecimal) …48 83 EC 08 BF A7 02 04 00E8 08 4A 04 00 31 C0 4883 C4 08 C3 ……(code from stdio.o) …48 65 6C 6C 6F 2C 20 57 6F72 6C 00 ……(data from stdio.o) …
hello.exe+ stdio.o
7
![Page 15: Brief Assembly Refreshercr4bd/3330/S2018/... · 1/23/2018 · lasttime processors↔memory,I/Odevices processor:sendaddresses(+values,sometimes) memory:replywithvalues(orstorevalue)](https://reader035.fdocuments.us/reader035/viewer/2022071405/60fa543e9dbbf135bd73dc1a/html5/thumbnails/15.jpg)
what’s in those files?#include <stdio.h>int main(void) {
puts("Hello, World!");return 0;
}
hello.c.text
main:sub $8, %rspmov $.Lstr, %rdicall putsxor %eax, %eaxadd $8, %rspret
.data.Lstr: .string "Hello,␣World!"
hello.s
text (code) segment:48 83 EC 08 BF 00 00 00 00 E8 00 0000 00 31 C0 48 83 C4 08 C3data segment:48 65 6C 6C 6F 2C 20 57 6F 72 6C 00relocations:
take 0s at and replace withtext, byte 6 ( ) data segment, byte 0text, byte 10 ( ) address of puts
symbol table:main text byte 0
hello.o
(actually binary, but shown as hexadecimal) …48 83 EC 08 BF A7 02 04 00E8 08 4A 04 00 31 C0 4883 C4 08 C3 ……(code from stdio.o) …48 65 6C 6C 6F 2C 20 57 6F72 6C 00 ……(data from stdio.o) …
hello.exe+ stdio.o
7
![Page 16: Brief Assembly Refreshercr4bd/3330/S2018/... · 1/23/2018 · lasttime processors↔memory,I/Odevices processor:sendaddresses(+values,sometimes) memory:replywithvalues(orstorevalue)](https://reader035.fdocuments.us/reader035/viewer/2022071405/60fa543e9dbbf135bd73dc1a/html5/thumbnails/16.jpg)
what’s in those files?#include <stdio.h>int main(void) {
puts("Hello, World!");return 0;
}
hello.c.text
main:sub $8, %rspmov $.Lstr, %rdicall putsxor %eax, %eaxadd $8, %rspret
.data.Lstr: .string "Hello,␣World!"
hello.s
text (code) segment:48 83 EC 08 BF 00 00 00 00 E8 00 0000 00 31 C0 48 83 C4 08 C3data segment:48 65 6C 6C 6F 2C 20 57 6F 72 6C 00relocations:
take 0s at and replace withtext, byte 6 ( ) data segment, byte 0text, byte 10 ( ) address of puts
symbol table:main text byte 0
hello.o
(actually binary, but shown as hexadecimal) …48 83 EC 08 BF A7 02 04 00E8 08 4A 04 00 31 C0 4883 C4 08 C3 ……(code from stdio.o) …48 65 6C 6C 6F 2C 20 57 6F72 6C 00 ……(data from stdio.o) …
hello.exe+ stdio.o
7
![Page 17: Brief Assembly Refreshercr4bd/3330/S2018/... · 1/23/2018 · lasttime processors↔memory,I/Odevices processor:sendaddresses(+values,sometimes) memory:replywithvalues(orstorevalue)](https://reader035.fdocuments.us/reader035/viewer/2022071405/60fa543e9dbbf135bd73dc1a/html5/thumbnails/17.jpg)
what’s in those files?#include <stdio.h>int main(void) {
puts("Hello, World!");return 0;
}
hello.c.text
main:sub $8, %rspmov $.Lstr, %rdicall putsxor %eax, %eaxadd $8, %rspret
.data.Lstr: .string "Hello,␣World!"
hello.s
text (code) segment:48 83 EC 08 BF 00 00 00 00 E8 00 0000 00 31 C0 48 83 C4 08 C3data segment:48 65 6C 6C 6F 2C 20 57 6F 72 6C 00relocations:
take 0s at and replace withtext, byte 6 ( ) data segment, byte 0text, byte 10 ( ) address of puts
symbol table:main text byte 0
hello.o
(actually binary, but shown as hexadecimal) …48 83 EC 08 BF A7 02 04 00E8 08 4A 04 00 31 C0 4883 C4 08 C3 ……(code from stdio.o) …48 65 6C 6C 6F 2C 20 57 6F72 6C 00 ……(data from stdio.o) …
hello.exe+ stdio.o
7
![Page 18: Brief Assembly Refreshercr4bd/3330/S2018/... · 1/23/2018 · lasttime processors↔memory,I/Odevices processor:sendaddresses(+values,sometimes) memory:replywithvalues(orstorevalue)](https://reader035.fdocuments.us/reader035/viewer/2022071405/60fa543e9dbbf135bd73dc1a/html5/thumbnails/18.jpg)
what’s in those files?#include <stdio.h>int main(void) {
puts("Hello, World!");return 0;
}
hello.c.text
main:sub $8, %rspmov $.Lstr, %rdicall putsxor %eax, %eaxadd $8, %rspret
.data.Lstr: .string "Hello,␣World!"
hello.s
text (code) segment:48 83 EC 08 BF 00 00 00 00 E8 00 0000 00 31 C0 48 83 C4 08 C3data segment:48 65 6C 6C 6F 2C 20 57 6F 72 6C 00relocations:
take 0s at and replace withtext, byte 6 ( ) data segment, byte 0text, byte 10 ( ) address of puts
symbol table:main text byte 0
hello.o
(actually binary, but shown as hexadecimal) …48 83 EC 08 BF A7 02 04 00E8 08 4A 04 00 31 C0 4883 C4 08 C3 ……(code from stdio.o) …48 65 6C 6C 6F 2C 20 57 6F72 6C 00 ……(data from stdio.o) …
hello.exe+ stdio.o
7
![Page 19: Brief Assembly Refreshercr4bd/3330/S2018/... · 1/23/2018 · lasttime processors↔memory,I/Odevices processor:sendaddresses(+values,sometimes) memory:replywithvalues(orstorevalue)](https://reader035.fdocuments.us/reader035/viewer/2022071405/60fa543e9dbbf135bd73dc1a/html5/thumbnails/19.jpg)
hello.s
.section .rodata.str1.1,"aMS",@progbits,1.LC0:
.string "Hello, World!"
.text
.globl mainmain:
subq $8, %rspmovl $.LC0, %edicall putsmovl $0, %eaxaddq $8, %rspret
8
![Page 20: Brief Assembly Refreshercr4bd/3330/S2018/... · 1/23/2018 · lasttime processors↔memory,I/Odevices processor:sendaddresses(+values,sometimes) memory:replywithvalues(orstorevalue)](https://reader035.fdocuments.us/reader035/viewer/2022071405/60fa543e9dbbf135bd73dc1a/html5/thumbnails/20.jpg)
exercise (1)
main.c:
1 #include <stdio.h>2 void sayHello(void) {3 puts("Hello, World!");4 }5 int main(void) {6 sayHello();7 }
Which files contain the memory address of sayHello?A. main.s (assembly) D. B and CB. main.o (object) E. A, B and CC. main.exe (executable) F. something else
9
![Page 21: Brief Assembly Refreshercr4bd/3330/S2018/... · 1/23/2018 · lasttime processors↔memory,I/Odevices processor:sendaddresses(+values,sometimes) memory:replywithvalues(orstorevalue)](https://reader035.fdocuments.us/reader035/viewer/2022071405/60fa543e9dbbf135bd73dc1a/html5/thumbnails/21.jpg)
exercise (2)
main.c:
1 #include <stdio.h>2 void sayHello(void) {3 puts("Hello, World!");4 }5 int main(void) {6 sayHello();7 }
Which files contain the literal ASCII string of Hello, World!?A. main.s (assembly) D. B and CB. main.o (object) E. A, B and CC. main.exe (executable) F. something else
10
![Page 22: Brief Assembly Refreshercr4bd/3330/S2018/... · 1/23/2018 · lasttime processors↔memory,I/Odevices processor:sendaddresses(+values,sometimes) memory:replywithvalues(orstorevalue)](https://reader035.fdocuments.us/reader035/viewer/2022071405/60fa543e9dbbf135bd73dc1a/html5/thumbnails/22.jpg)
relocation types
machine code doesn’t always use addresses as is
“call function 4303 bytes later”
linker needs to compute “4303”extra field on relocation list
11
![Page 23: Brief Assembly Refreshercr4bd/3330/S2018/... · 1/23/2018 · lasttime processors↔memory,I/Odevices processor:sendaddresses(+values,sometimes) memory:replywithvalues(orstorevalue)](https://reader035.fdocuments.us/reader035/viewer/2022071405/60fa543e9dbbf135bd73dc1a/html5/thumbnails/23.jpg)
dynamic linking (very briefly)
dynamic linking — done when application is loadedidea: don’t have N copies of printfother type of linking: static (gcc -static)
often extra indirection:
call functionTable[number_for_printf]
linker fills in functionTable instead of changing calls
12
![Page 24: Brief Assembly Refreshercr4bd/3330/S2018/... · 1/23/2018 · lasttime processors↔memory,I/Odevices processor:sendaddresses(+values,sometimes) memory:replywithvalues(orstorevalue)](https://reader035.fdocuments.us/reader035/viewer/2022071405/60fa543e9dbbf135bd73dc1a/html5/thumbnails/24.jpg)
ldd /bin/ls
$ ldd /bin/lslinux-vdso.so.1 => (0x00007ffcca9d8000)libselinux.so.1 => /lib/x86_64-linux-gnu/libselinux.so.1
(0x00007f851756f000)libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6
(0x00007f85171a5000)libpcre.so.3 => /lib/x86_64-linux-gnu/libpcre.so.3
(0x00007f8516f35000)libdl.so.2 => /lib/x86_64-linux-gnu/libdl.so.2
(0x00007f8516d31000)/lib64/ld-linux-x86-64.so.2 (0x00007f8517791000)libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0
(0x00007f8516b14000)
13
![Page 25: Brief Assembly Refreshercr4bd/3330/S2018/... · 1/23/2018 · lasttime processors↔memory,I/Odevices processor:sendaddresses(+values,sometimes) memory:replywithvalues(orstorevalue)](https://reader035.fdocuments.us/reader035/viewer/2022071405/60fa543e9dbbf135bd73dc1a/html5/thumbnails/25.jpg)
layers of abstraction
“Higher-level” language: Cx += y
Assembly: X86-64add %rbx, %rax
Machine code: Y8660 03SIXTEEN
Hardware Design Language: HCLRS
Gates / Transistors / Wires / Registers
14
![Page 26: Brief Assembly Refreshercr4bd/3330/S2018/... · 1/23/2018 · lasttime processors↔memory,I/Odevices processor:sendaddresses(+values,sometimes) memory:replywithvalues(orstorevalue)](https://reader035.fdocuments.us/reader035/viewer/2022071405/60fa543e9dbbf135bd73dc1a/html5/thumbnails/26.jpg)
AT&T versus Intel syntax (1)
AT&T syntax:movq $42, (%rbx)
Intel syntax:mov QWORD PTR [rbx], 42
effect (pseudo-C):memory[rbx] <- 42
15
![Page 27: Brief Assembly Refreshercr4bd/3330/S2018/... · 1/23/2018 · lasttime processors↔memory,I/Odevices processor:sendaddresses(+values,sometimes) memory:replywithvalues(orstorevalue)](https://reader035.fdocuments.us/reader035/viewer/2022071405/60fa543e9dbbf135bd73dc1a/html5/thumbnails/27.jpg)
AT&T syntax example (1)
movq $42, (%rbx)// memory[rbx] ← 42
destination last
()s represent value in memory
constants start with $
registers start with %
q (‘quad’) indicates length (8 bytes)l: 4; w: 2; b: 1sometimes can be omitted
16
![Page 28: Brief Assembly Refreshercr4bd/3330/S2018/... · 1/23/2018 · lasttime processors↔memory,I/Odevices processor:sendaddresses(+values,sometimes) memory:replywithvalues(orstorevalue)](https://reader035.fdocuments.us/reader035/viewer/2022071405/60fa543e9dbbf135bd73dc1a/html5/thumbnails/28.jpg)
AT&T syntax example (1)
movq $42, (%rbx)// memory[rbx] ← 42
destination last
()s represent value in memory
constants start with $
registers start with %
q (‘quad’) indicates length (8 bytes)l: 4; w: 2; b: 1sometimes can be omitted
16
![Page 29: Brief Assembly Refreshercr4bd/3330/S2018/... · 1/23/2018 · lasttime processors↔memory,I/Odevices processor:sendaddresses(+values,sometimes) memory:replywithvalues(orstorevalue)](https://reader035.fdocuments.us/reader035/viewer/2022071405/60fa543e9dbbf135bd73dc1a/html5/thumbnails/29.jpg)
AT&T syntax example (1)
movq $42, (%rbx)// memory[rbx] ← 42
destination last
()s represent value in memory
constants start with $
registers start with %
q (‘quad’) indicates length (8 bytes)l: 4; w: 2; b: 1sometimes can be omitted
16
![Page 30: Brief Assembly Refreshercr4bd/3330/S2018/... · 1/23/2018 · lasttime processors↔memory,I/Odevices processor:sendaddresses(+values,sometimes) memory:replywithvalues(orstorevalue)](https://reader035.fdocuments.us/reader035/viewer/2022071405/60fa543e9dbbf135bd73dc1a/html5/thumbnails/30.jpg)
AT&T syntax example (1)
movq $42, (%rbx)// memory[rbx] ← 42
destination last
()s represent value in memory
constants start with $
registers start with %
q (‘quad’) indicates length (8 bytes)l: 4; w: 2; b: 1sometimes can be omitted
16
![Page 31: Brief Assembly Refreshercr4bd/3330/S2018/... · 1/23/2018 · lasttime processors↔memory,I/Odevices processor:sendaddresses(+values,sometimes) memory:replywithvalues(orstorevalue)](https://reader035.fdocuments.us/reader035/viewer/2022071405/60fa543e9dbbf135bd73dc1a/html5/thumbnails/31.jpg)
AT&T syntax example (1)
movq $42, (%rbx)// memory[rbx] ← 42
destination last
()s represent value in memory
constants start with $
registers start with %
q (‘quad’) indicates length (8 bytes)l: 4; w: 2; b: 1sometimes can be omitted
16
![Page 32: Brief Assembly Refreshercr4bd/3330/S2018/... · 1/23/2018 · lasttime processors↔memory,I/Odevices processor:sendaddresses(+values,sometimes) memory:replywithvalues(orstorevalue)](https://reader035.fdocuments.us/reader035/viewer/2022071405/60fa543e9dbbf135bd73dc1a/html5/thumbnails/32.jpg)
AT&T versus Intel syntax (2)
AT&T syntax:movq $42, 100(%rbx,%rcx,4)
Intel syntax:mov QWORD PTR [rbx+rcx*4+100], 42
effect (pseudo-C):memory[rbx + rcx * 4 + 100] <- 42
17
![Page 33: Brief Assembly Refreshercr4bd/3330/S2018/... · 1/23/2018 · lasttime processors↔memory,I/Odevices processor:sendaddresses(+values,sometimes) memory:replywithvalues(orstorevalue)](https://reader035.fdocuments.us/reader035/viewer/2022071405/60fa543e9dbbf135bd73dc1a/html5/thumbnails/33.jpg)
AT&T versus Intel syntax (2)
AT&T syntax:movq $42, 100(%rbx,%rcx,4)
Intel syntax:mov QWORD PTR [rbx+rcx*4+100], 42
effect (pseudo-C):memory[rbx + rcx * 4 + 100] <- 42
17
![Page 34: Brief Assembly Refreshercr4bd/3330/S2018/... · 1/23/2018 · lasttime processors↔memory,I/Odevices processor:sendaddresses(+values,sometimes) memory:replywithvalues(orstorevalue)](https://reader035.fdocuments.us/reader035/viewer/2022071405/60fa543e9dbbf135bd73dc1a/html5/thumbnails/34.jpg)
AT&T versus Intel syntax (2)
AT&T syntax:movq $42, 100(%rbx,%rcx,4)
Intel syntax:mov QWORD PTR [rbx+rcx*4+100], 42
effect (pseudo-C):memory[rbx + rcx * 4 + 100] <- 42
17
![Page 35: Brief Assembly Refreshercr4bd/3330/S2018/... · 1/23/2018 · lasttime processors↔memory,I/Odevices processor:sendaddresses(+values,sometimes) memory:replywithvalues(orstorevalue)](https://reader035.fdocuments.us/reader035/viewer/2022071405/60fa543e9dbbf135bd73dc1a/html5/thumbnails/35.jpg)
AT&T versus Intel syntax (2)
AT&T syntax:movq $42, 100(%rbx,%rcx,4)
Intel syntax:mov QWORD PTR [rbx+rcx*4+100], 42
effect (pseudo-C):memory[rbx + rcx * 4 + 100] <- 42
17
![Page 36: Brief Assembly Refreshercr4bd/3330/S2018/... · 1/23/2018 · lasttime processors↔memory,I/Odevices processor:sendaddresses(+values,sometimes) memory:replywithvalues(orstorevalue)](https://reader035.fdocuments.us/reader035/viewer/2022071405/60fa543e9dbbf135bd73dc1a/html5/thumbnails/36.jpg)
AT&T syntax: addressing
100(%rbx): memory[rbx + 100]
100(%rbx,8): memory[rbx * 8 + 100]
100(,%rbx,8): memory[rbx * 8 + 100]
100(%rcx,%rbx,8):memory[rcx + rbx * 8 + 100]
100:memory[100]
100(%rbx,%rcx):memory[rbx+rcx+100]
18
![Page 37: Brief Assembly Refreshercr4bd/3330/S2018/... · 1/23/2018 · lasttime processors↔memory,I/Odevices processor:sendaddresses(+values,sometimes) memory:replywithvalues(orstorevalue)](https://reader035.fdocuments.us/reader035/viewer/2022071405/60fa543e9dbbf135bd73dc1a/html5/thumbnails/37.jpg)
AT&T versus Intel syntax (3)
r8 ← r8 - raxIntel syntax: sub r8, raxAT&T syntax: subq %rax, %r8
same for cmpq
19
![Page 38: Brief Assembly Refreshercr4bd/3330/S2018/... · 1/23/2018 · lasttime processors↔memory,I/Odevices processor:sendaddresses(+values,sometimes) memory:replywithvalues(orstorevalue)](https://reader035.fdocuments.us/reader035/viewer/2022071405/60fa543e9dbbf135bd73dc1a/html5/thumbnails/38.jpg)
AT&T syntax: addresses
addq 0x1000, %rax// Intel syntax: add rax, QWORD PTR [0x1000]// rax ← rax + memory[0x1000]addq $0x1000, %rax// Intel syntax: add rax, 0x1000// rax ← rax + 0x1000
no $ — probably memory address
20
![Page 39: Brief Assembly Refreshercr4bd/3330/S2018/... · 1/23/2018 · lasttime processors↔memory,I/Odevices processor:sendaddresses(+values,sometimes) memory:replywithvalues(orstorevalue)](https://reader035.fdocuments.us/reader035/viewer/2022071405/60fa543e9dbbf135bd73dc1a/html5/thumbnails/39.jpg)
AT&T syntax in one slide
destination last
() means value in memory
disp(base, index, scale) same asmemory[disp + base + index * scale]
omit disp (defaults to 0)and/or omit base (defaults to 0)and/or scale (defualts to 1)
$ means constant
plain number/label means value in memory
21
![Page 40: Brief Assembly Refreshercr4bd/3330/S2018/... · 1/23/2018 · lasttime processors↔memory,I/Odevices processor:sendaddresses(+values,sometimes) memory:replywithvalues(orstorevalue)](https://reader035.fdocuments.us/reader035/viewer/2022071405/60fa543e9dbbf135bd73dc1a/html5/thumbnails/40.jpg)
extra detail: computed jumps
jmpq *%rax// Intel syntax: jmp RAX
// goto RAXjmpq *1000(%rax,%rbx,8)// Intel syntax: jmp QWORD PTR[RAX+RBX*8+1000]
// read address from memory at RAX + RBX * 8 + 1000// go to that address
22
![Page 41: Brief Assembly Refreshercr4bd/3330/S2018/... · 1/23/2018 · lasttime processors↔memory,I/Odevices processor:sendaddresses(+values,sometimes) memory:replywithvalues(orstorevalue)](https://reader035.fdocuments.us/reader035/viewer/2022071405/60fa543e9dbbf135bd73dc1a/html5/thumbnails/41.jpg)
recall: x86-64 general purpose registers
Figure: Immae via Wikipedia 23
![Page 42: Brief Assembly Refreshercr4bd/3330/S2018/... · 1/23/2018 · lasttime processors↔memory,I/Odevices processor:sendaddresses(+values,sometimes) memory:replywithvalues(orstorevalue)](https://reader035.fdocuments.us/reader035/viewer/2022071405/60fa543e9dbbf135bd73dc1a/html5/thumbnails/42.jpg)
overlapping registers (1)
setting 32-bit registers — clears corresponding 64-bit registermovq $0xFFFFFFFFFFFFFFFF, %raxmovl $0x1, %eax
%rax is 0x1 (not 0xFFFFFFFF00000001)
24
![Page 43: Brief Assembly Refreshercr4bd/3330/S2018/... · 1/23/2018 · lasttime processors↔memory,I/Odevices processor:sendaddresses(+values,sometimes) memory:replywithvalues(orstorevalue)](https://reader035.fdocuments.us/reader035/viewer/2022071405/60fa543e9dbbf135bd73dc1a/html5/thumbnails/43.jpg)
overlapping registers (2)
setting 8/16-bit registers: don’t clear 64-bit registermovq $0xFFFFFFFFFFFFFFFF, %raxmovb $0x1, %al
%rax is 0xFFFFFFFFFFFFFF01
25
![Page 44: Brief Assembly Refreshercr4bd/3330/S2018/... · 1/23/2018 · lasttime processors↔memory,I/Odevices processor:sendaddresses(+values,sometimes) memory:replywithvalues(orstorevalue)](https://reader035.fdocuments.us/reader035/viewer/2022071405/60fa543e9dbbf135bd73dc1a/html5/thumbnails/44.jpg)
labels (1)
labels represent addresses
26
![Page 45: Brief Assembly Refreshercr4bd/3330/S2018/... · 1/23/2018 · lasttime processors↔memory,I/Odevices processor:sendaddresses(+values,sometimes) memory:replywithvalues(orstorevalue)](https://reader035.fdocuments.us/reader035/viewer/2022071405/60fa543e9dbbf135bd73dc1a/html5/thumbnails/45.jpg)
labels (2)
addq string, %rax// intel syntax: add rax, QWORD PTR [label]// rax ← rax + memory[address of "a string"]addq $string, %rax// intel syntax: add rax, OFFSET label// rax ← rax + address of "a string"
string: .ascii "a␣string"
addq label: read value at the address
addq $label: use address as an integer constant
27
![Page 46: Brief Assembly Refreshercr4bd/3330/S2018/... · 1/23/2018 · lasttime processors↔memory,I/Odevices processor:sendaddresses(+values,sometimes) memory:replywithvalues(orstorevalue)](https://reader035.fdocuments.us/reader035/viewer/2022071405/60fa543e9dbbf135bd73dc1a/html5/thumbnails/46.jpg)
on LEA
LEA = Load Effective Addresseffective address = computed address for memory access
syntax looks like a mov from memory, but…
skips the memory access — just uses the address(sort of like & operator in C?)
leaq 4(%rax), %rax ≈ addq $4, %rax
“address of memory[rax + 4]” = rax + 4
29
![Page 47: Brief Assembly Refreshercr4bd/3330/S2018/... · 1/23/2018 · lasttime processors↔memory,I/Odevices processor:sendaddresses(+values,sometimes) memory:replywithvalues(orstorevalue)](https://reader035.fdocuments.us/reader035/viewer/2022071405/60fa543e9dbbf135bd73dc1a/html5/thumbnails/47.jpg)
on LEA
LEA = Load Effective Addresseffective address = computed address for memory access
syntax looks like a mov from memory, but…
skips the memory access — just uses the address(sort of like & operator in C?)
leaq 4(%rax), %rax ≈ addq $4, %rax
“address of memory[rax + 4]” = rax + 4
29
![Page 48: Brief Assembly Refreshercr4bd/3330/S2018/... · 1/23/2018 · lasttime processors↔memory,I/Odevices processor:sendaddresses(+values,sometimes) memory:replywithvalues(orstorevalue)](https://reader035.fdocuments.us/reader035/viewer/2022071405/60fa543e9dbbf135bd73dc1a/html5/thumbnails/48.jpg)
LEA tricks
leaq (%rax,%rax,4), %rax
rax ← rax × 5
rax ← address-of(memory[rax + rax * 4])
leaq (%rbx,%rcx), %rdx
rdx ← rbx + rcx
rdx ←address-of(memory[rbx + rcx])
30
![Page 49: Brief Assembly Refreshercr4bd/3330/S2018/... · 1/23/2018 · lasttime processors↔memory,I/Odevices processor:sendaddresses(+values,sometimes) memory:replywithvalues(orstorevalue)](https://reader035.fdocuments.us/reader035/viewer/2022071405/60fa543e9dbbf135bd73dc1a/html5/thumbnails/49.jpg)
exercise: what is this function?
mystery:leal 0(,%rdi,8), %eaxsubl %edi, %eaxret
int mystery(int arg) { return ...; }A. arg * 9 D. -arg * 7B. -arg * 9 E. none of theseC. arg * 8 F. it has a different prototype
31
![Page 50: Brief Assembly Refreshercr4bd/3330/S2018/... · 1/23/2018 · lasttime processors↔memory,I/Odevices processor:sendaddresses(+values,sometimes) memory:replywithvalues(orstorevalue)](https://reader035.fdocuments.us/reader035/viewer/2022071405/60fa543e9dbbf135bd73dc1a/html5/thumbnails/50.jpg)
exercise: what is this function?
mystery:leal 0(,%rdi,8), %eaxsubl %edi, %eaxret
int mystery(int arg) { return ...; }A. arg * 9 D. -arg * 7B. -arg * 9 E. none of theseC. arg * 8 F. it has a different prototype
31
![Page 51: Brief Assembly Refreshercr4bd/3330/S2018/... · 1/23/2018 · lasttime processors↔memory,I/Odevices processor:sendaddresses(+values,sometimes) memory:replywithvalues(orstorevalue)](https://reader035.fdocuments.us/reader035/viewer/2022071405/60fa543e9dbbf135bd73dc1a/html5/thumbnails/51.jpg)
Linux x86-64 calling convention
registers for first 6 arguments:%rdi (or %edi or %di, etc.), then%rsi (or %esi or %si, etc.), then%rdx (or %edx or %dx, etc.), then%rcx (or %ecx or %cx, etc.), then%r8 (or %r8d or %r8w, etc.), then%r9 (or %r9d or %r9w, etc.)
rest on stack
return value in %rax
don’t memorize: Figure 3.28 in book
32
![Page 52: Brief Assembly Refreshercr4bd/3330/S2018/... · 1/23/2018 · lasttime processors↔memory,I/Odevices processor:sendaddresses(+values,sometimes) memory:replywithvalues(orstorevalue)](https://reader035.fdocuments.us/reader035/viewer/2022071405/60fa543e9dbbf135bd73dc1a/html5/thumbnails/52.jpg)
x86-64 calling convention example
int foo(int x, int y, int z) { return 42; }...
foo(1, 2, 3);......
// foo(1, 2, 3)movl $1, %edimovl $2, %esimovl $3, %edxcall foo // call pushes address of next instruction
// then jumps to foo...foo:
movl $42, %eaxret
33
![Page 53: Brief Assembly Refreshercr4bd/3330/S2018/... · 1/23/2018 · lasttime processors↔memory,I/Odevices processor:sendaddresses(+values,sometimes) memory:replywithvalues(orstorevalue)](https://reader035.fdocuments.us/reader035/viewer/2022071405/60fa543e9dbbf135bd73dc1a/html5/thumbnails/53.jpg)
call/ret
call:push address of next instruction on the stack
ret:pop address from stack; jump
34
![Page 54: Brief Assembly Refreshercr4bd/3330/S2018/... · 1/23/2018 · lasttime processors↔memory,I/Odevices processor:sendaddresses(+values,sometimes) memory:replywithvalues(orstorevalue)](https://reader035.fdocuments.us/reader035/viewer/2022071405/60fa543e9dbbf135bd73dc1a/html5/thumbnails/54.jpg)
callee-saved registers
functions must preserve these
%rsp (stack pointer), %rbx, %rbp (frame pointer, maybe)
%r12-%r15
35
![Page 55: Brief Assembly Refreshercr4bd/3330/S2018/... · 1/23/2018 · lasttime processors↔memory,I/Odevices processor:sendaddresses(+values,sometimes) memory:replywithvalues(orstorevalue)](https://reader035.fdocuments.us/reader035/viewer/2022071405/60fa543e9dbbf135bd73dc1a/html5/thumbnails/55.jpg)
caller/callee-saved
foo:pushq %r12 // r12 is callee-saved... use r12 ...popq %r12ret
...other_function:
...pushq %r11 // r11 is caller-savedcallq foopopq %r11
36
![Page 56: Brief Assembly Refreshercr4bd/3330/S2018/... · 1/23/2018 · lasttime processors↔memory,I/Odevices processor:sendaddresses(+values,sometimes) memory:replywithvalues(orstorevalue)](https://reader035.fdocuments.us/reader035/viewer/2022071405/60fa543e9dbbf135bd73dc1a/html5/thumbnails/56.jpg)
question
pushq $0x1pushq $0x2addq $0x3, 8(%rsp)popq %raxpopq %rbx
What is value of %rax and %rbx after this?a. %rax = 0x2, %rbx = 0x4b. %rax = 0x5, %rbx = 0x1c. %rax = 0x2, %rbx = 0x1d. the snippet has invalid syntax or will crashe. more information is neededf. something else?
37
![Page 57: Brief Assembly Refreshercr4bd/3330/S2018/... · 1/23/2018 · lasttime processors↔memory,I/Odevices processor:sendaddresses(+values,sometimes) memory:replywithvalues(orstorevalue)](https://reader035.fdocuments.us/reader035/viewer/2022071405/60fa543e9dbbf135bd73dc1a/html5/thumbnails/57.jpg)
on %rip
%rip (Instruction Pointer) = address of next instruction
movq 500(%rip), %raxrax ← memory[next instruction address + 500]
label(%rip) ≈ labeldifferent ways of writing address of label in machine code(with %rip — relative to next instruction)
38
![Page 58: Brief Assembly Refreshercr4bd/3330/S2018/... · 1/23/2018 · lasttime processors↔memory,I/Odevices processor:sendaddresses(+values,sometimes) memory:replywithvalues(orstorevalue)](https://reader035.fdocuments.us/reader035/viewer/2022071405/60fa543e9dbbf135bd73dc1a/html5/thumbnails/58.jpg)
on %rip
%rip (Instruction Pointer) = address of next instruction
movq 500(%rip), %raxrax ← memory[next instruction address + 500]
label(%rip) ≈ labeldifferent ways of writing address of label in machine code(with %rip — relative to next instruction)
38
![Page 59: Brief Assembly Refreshercr4bd/3330/S2018/... · 1/23/2018 · lasttime processors↔memory,I/Odevices processor:sendaddresses(+values,sometimes) memory:replywithvalues(orstorevalue)](https://reader035.fdocuments.us/reader035/viewer/2022071405/60fa543e9dbbf135bd73dc1a/html5/thumbnails/59.jpg)
things we won’t cover (today)
floating point; vector operations (multiple values at once)special registers: %xmm0 through %xmm15
segmentation (special registers: %ds, %fs, %gs, …)
lots and lots of instructions
39
![Page 60: Brief Assembly Refreshercr4bd/3330/S2018/... · 1/23/2018 · lasttime processors↔memory,I/Odevices processor:sendaddresses(+values,sometimes) memory:replywithvalues(orstorevalue)](https://reader035.fdocuments.us/reader035/viewer/2022071405/60fa543e9dbbf135bd73dc1a/html5/thumbnails/60.jpg)
authoriative source (1)
40
![Page 61: Brief Assembly Refreshercr4bd/3330/S2018/... · 1/23/2018 · lasttime processors↔memory,I/Odevices processor:sendaddresses(+values,sometimes) memory:replywithvalues(orstorevalue)](https://reader035.fdocuments.us/reader035/viewer/2022071405/60fa543e9dbbf135bd73dc1a/html5/thumbnails/61.jpg)
authoriative source (2)
41
![Page 62: Brief Assembly Refreshercr4bd/3330/S2018/... · 1/23/2018 · lasttime processors↔memory,I/Odevices processor:sendaddresses(+values,sometimes) memory:replywithvalues(orstorevalue)](https://reader035.fdocuments.us/reader035/viewer/2022071405/60fa543e9dbbf135bd73dc1a/html5/thumbnails/62.jpg)
goto
for (...) {for (...) {if (thingAt(i, j)) {
goto found;}
}}printf("not found!\n");return;
found:printf("found!\n");
assembly:jmp found
assembly:found:
42
![Page 63: Brief Assembly Refreshercr4bd/3330/S2018/... · 1/23/2018 · lasttime processors↔memory,I/Odevices processor:sendaddresses(+values,sometimes) memory:replywithvalues(orstorevalue)](https://reader035.fdocuments.us/reader035/viewer/2022071405/60fa543e9dbbf135bd73dc1a/html5/thumbnails/63.jpg)
goto
for (...) {for (...) {if (thingAt(i, j)) {
goto found;}
}}printf("not found!\n");return;
found:printf("found!\n");
assembly:jmp found
assembly:found:
42
![Page 64: Brief Assembly Refreshercr4bd/3330/S2018/... · 1/23/2018 · lasttime processors↔memory,I/Odevices processor:sendaddresses(+values,sometimes) memory:replywithvalues(orstorevalue)](https://reader035.fdocuments.us/reader035/viewer/2022071405/60fa543e9dbbf135bd73dc1a/html5/thumbnails/64.jpg)
if-to-assembly (1)
if (b >= 42) {a += 10;
} else {a *= b;
}
if (b < 42) goto after_then;a += 10;goto after_else;
after_then: a *= b;after_else:
43
![Page 65: Brief Assembly Refreshercr4bd/3330/S2018/... · 1/23/2018 · lasttime processors↔memory,I/Odevices processor:sendaddresses(+values,sometimes) memory:replywithvalues(orstorevalue)](https://reader035.fdocuments.us/reader035/viewer/2022071405/60fa543e9dbbf135bd73dc1a/html5/thumbnails/65.jpg)
if-to-assembly (1)
if (b >= 42) {a += 10;
} else {a *= b;
}
if (b < 42) goto after_then;a += 10;goto after_else;
after_then: a *= b;after_else:
43
![Page 66: Brief Assembly Refreshercr4bd/3330/S2018/... · 1/23/2018 · lasttime processors↔memory,I/Odevices processor:sendaddresses(+values,sometimes) memory:replywithvalues(orstorevalue)](https://reader035.fdocuments.us/reader035/viewer/2022071405/60fa543e9dbbf135bd73dc1a/html5/thumbnails/66.jpg)
if-to-assembly (2)
if (b >= 42) {a += 10;
} else {a *= b;
}// a is in %rax, b is in %rbx
cmpq $42, %rbx // computes rbx - 42 to 0jl after_then // jump if rbx - 42 < 0
// AKA rbx < 42addq $10, %rax // a += 1jmp after_else
after_then:imulq %rbx, %rax // rax = rax * rbx
after_else:44
![Page 67: Brief Assembly Refreshercr4bd/3330/S2018/... · 1/23/2018 · lasttime processors↔memory,I/Odevices processor:sendaddresses(+values,sometimes) memory:replywithvalues(orstorevalue)](https://reader035.fdocuments.us/reader035/viewer/2022071405/60fa543e9dbbf135bd73dc1a/html5/thumbnails/67.jpg)
do-while-to-assembly (1)
int x = 99;do {
foo()x--;
} while (x >= 0);
int x = 99;start_loop:
foo()x--;if (x >= 0) goto start_loop;
45
![Page 68: Brief Assembly Refreshercr4bd/3330/S2018/... · 1/23/2018 · lasttime processors↔memory,I/Odevices processor:sendaddresses(+values,sometimes) memory:replywithvalues(orstorevalue)](https://reader035.fdocuments.us/reader035/viewer/2022071405/60fa543e9dbbf135bd73dc1a/html5/thumbnails/68.jpg)
do-while-to-assembly (1)
int x = 99;do {
foo()x--;
} while (x >= 0);
int x = 99;start_loop:
foo()x--;if (x >= 0) goto start_loop;
45
![Page 69: Brief Assembly Refreshercr4bd/3330/S2018/... · 1/23/2018 · lasttime processors↔memory,I/Odevices processor:sendaddresses(+values,sometimes) memory:replywithvalues(orstorevalue)](https://reader035.fdocuments.us/reader035/viewer/2022071405/60fa543e9dbbf135bd73dc1a/html5/thumbnails/69.jpg)
do-while-to-assembly (2)
int x = 99;do {
foo()x--;
} while (x >= 0);
movq $99, %r12 // register for xstart_loop:
call foosubq $1, %r12cmpq $0, %r12// computes r12 - 0 = r12jge start_loop // jump if r12 - 0 >= 0
46
![Page 70: Brief Assembly Refreshercr4bd/3330/S2018/... · 1/23/2018 · lasttime processors↔memory,I/Odevices processor:sendaddresses(+values,sometimes) memory:replywithvalues(orstorevalue)](https://reader035.fdocuments.us/reader035/viewer/2022071405/60fa543e9dbbf135bd73dc1a/html5/thumbnails/70.jpg)
condition codes
x86 has condition codes
set by (almost) all arithmetic instructionsaddq, subq, imulq, etc.
store info about last arithmetic resultwas it zero? was it negative? etc.
47
![Page 71: Brief Assembly Refreshercr4bd/3330/S2018/... · 1/23/2018 · lasttime processors↔memory,I/Odevices processor:sendaddresses(+values,sometimes) memory:replywithvalues(orstorevalue)](https://reader035.fdocuments.us/reader035/viewer/2022071405/60fa543e9dbbf135bd73dc1a/html5/thumbnails/71.jpg)
condition codes and jumps
jg, jle, etc. read condition codes
named based on interpreting result of subtraction
0: equal; negative: less than; positive: greater than
48
![Page 72: Brief Assembly Refreshercr4bd/3330/S2018/... · 1/23/2018 · lasttime processors↔memory,I/Odevices processor:sendaddresses(+values,sometimes) memory:replywithvalues(orstorevalue)](https://reader035.fdocuments.us/reader035/viewer/2022071405/60fa543e9dbbf135bd73dc1a/html5/thumbnails/72.jpg)
condition codes example (1)
movq $−10, %raxmovq $20, %rbxsubq %rax, %rbx // %rbx - %rax = 30
// result > 0: %rbx was > %raxjle foo // not taken; 30 > 0
49
![Page 73: Brief Assembly Refreshercr4bd/3330/S2018/... · 1/23/2018 · lasttime processors↔memory,I/Odevices processor:sendaddresses(+values,sometimes) memory:replywithvalues(orstorevalue)](https://reader035.fdocuments.us/reader035/viewer/2022071405/60fa543e9dbbf135bd73dc1a/html5/thumbnails/73.jpg)
condition codes and cmpq
“last arithmetic result”???
then what is cmp, etc.?
cmp does subtraction (but doesn’t store result)
similar test does bitwise-and
testq %rax, %rax — result is %rax
50
![Page 74: Brief Assembly Refreshercr4bd/3330/S2018/... · 1/23/2018 · lasttime processors↔memory,I/Odevices processor:sendaddresses(+values,sometimes) memory:replywithvalues(orstorevalue)](https://reader035.fdocuments.us/reader035/viewer/2022071405/60fa543e9dbbf135bd73dc1a/html5/thumbnails/74.jpg)
condition codes example (2)
movq $−10, %raxmovq $20, %rbxcmpq %rax, %rbxjle foo // not taken; %rbx - %rax > 0
51
![Page 75: Brief Assembly Refreshercr4bd/3330/S2018/... · 1/23/2018 · lasttime processors↔memory,I/Odevices processor:sendaddresses(+values,sometimes) memory:replywithvalues(orstorevalue)](https://reader035.fdocuments.us/reader035/viewer/2022071405/60fa543e9dbbf135bd73dc1a/html5/thumbnails/75.jpg)
omitting the cmp
movq $99, %r12 // register for xstart_loop:
call foosubq $1, %r12cmpq $0, %r12// compute r12 - 0 + sets cond. codesjge start_loop // r12 >= 0?
// or result >= 0?
movq $99, %r12 // register for xstart_loop:
call foosubq $1, %r12// new r12 = old r12 - 1 + sets cond. codesjge start_loop // old r12 >= 1?
// or result >= 0?52
![Page 76: Brief Assembly Refreshercr4bd/3330/S2018/... · 1/23/2018 · lasttime processors↔memory,I/Odevices processor:sendaddresses(+values,sometimes) memory:replywithvalues(orstorevalue)](https://reader035.fdocuments.us/reader035/viewer/2022071405/60fa543e9dbbf135bd73dc1a/html5/thumbnails/76.jpg)
condition codes example (3)
movq $−10, %raxmovq $20, %rbxsubq %rax, %rbxjle foo // not taken, %rbx - %rax > 0 -> %rbx > %rax
movq $20, %rbxaddq $−20, %rbxje foo // taken, result is 0
// x - y = 0 -> x = y
53
![Page 77: Brief Assembly Refreshercr4bd/3330/S2018/... · 1/23/2018 · lasttime processors↔memory,I/Odevices processor:sendaddresses(+values,sometimes) memory:replywithvalues(orstorevalue)](https://reader035.fdocuments.us/reader035/viewer/2022071405/60fa543e9dbbf135bd73dc1a/html5/thumbnails/77.jpg)
what sets condition codes
most instructions that compute something set condition codes
some instructions only set condition codes:cmp ∼ subtest ∼ and (bitwise and — later)testq %rax, %rax — result is %rax
some instructions don’t change condition codes:lea, movcontrol flow: jmp, call, ret, jle, etc.
54
![Page 78: Brief Assembly Refreshercr4bd/3330/S2018/... · 1/23/2018 · lasttime processors↔memory,I/Odevices processor:sendaddresses(+values,sometimes) memory:replywithvalues(orstorevalue)](https://reader035.fdocuments.us/reader035/viewer/2022071405/60fa543e9dbbf135bd73dc1a/html5/thumbnails/78.jpg)
condition codes examples (4)
movq $20, %rbxaddq $−20, %rbx // result is 0movq $1, %rax // irrelevantje foo // taken, result is 0
20 + -20 = 0 — SF = 0 (not negative), ZF = 1 (zero)
55