the forgotten art of assembly
TRANSCRIPT
The lost art of AssemblyThe lost art of Assembly
I have learned Assembly 3 times :)
http://www.atmel.com/webdoc/avrassembler/avrassembler.wb_instruction_list.html
I have learned Assembly 3 times :)
I have learned Assembly 3 times :)
I have learned Assembly 3 times :)
Why Assembly?
Registers
EIP - Instruction Pointer
ESP - Stack Pointer
EBP - Frame base pointer (function base pointer)
EAX - RAX (sometimes counter)
EBX - RBX
ECX - RBX
ESI - RSI (source)
EDI - RDI (destination)
Registers
EXX - 32bit RXX - 64bit
XMM - 128bit YMM - 256bit ZMM - 512bit
AVX
512 - 256 ZMM0 - ZMM31
255 - 128 YMM0 - YMM31
127 - 0 XMM0 - XMM31
https://software.intel.com/en-us/isa-extensionshttps://software.intel.com/en-us/blogs/2013/avx-512-instructions
Basic instructions
● mov● add● jumps
– jcc
– jle
– jne
● int– call
– ret
int 0x80 Definition
int 0x80 is the assembly language instruction that is used to invoke system calls in Linux on x86 (i.e., Intel-compatible) processors.
http://www.linfo.org/int_0x80.html
Assembly vs. Disassemby :)
section .data
msg db "Hello, world!", 0x0a
section .text
global _start
_start:
; SYSCALL: write(1, msg, 14)
mov eax, 4
mov ebx, 1
mov ecx, msg
mov edx, 14
int 0x80
; SYSCALL: exit(0)
mov eax, 1
mov ebx, 0
int 0x80
Disassembly of section .text:
08048080 <_start>:
8048080: b8 04 00 00 00 mov eax,0x4
8048085: bb 01 00 00 00 mov ebx,0x1
804808a: b9 a4 90 04 08 mov ecx,0x80490a4
804808f: ba 0e 00 00 00 mov edx,0xe
8048094: cd 80 int 0x80
8048096: b8 01 00 00 00 mov eax,0x1
804809b: bb 00 00 00 00 mov ebx,0x0
80480a0: cd 80 int 0x80
Disassembly of section .data:
080490a4 <msg>:
80490a4: 48 dec eax
80490a5: 65 6c gs ins BYTE PTR es:[edi],dx
80490a7: 6c ins BYTE PTR es:[edi],dx
80490a8: 6f outs dx,DWORD PTR ds:[esi]
80490a9: 2c 20 sub al,0x20
80490ab: 77 6f ja 804911c <_end+0x68>
80490ad: 72 6c jb 804911b <_end+0x67>
80490af: 64 21 0a and DWORD PTR fs:[edx],ecx
48 65 6c 6c 6f 2c 20 77 6f 72 6c 64 21 0a = Hello, world!
https://www.rapidtables.com/convert/number/hex-to-ascii.html
DEMO time
The ELF format
● Executable and Linkable Format● 4bytes magic number● 32/64bit binary● instruction set architecture● interpreter for the program
https://en.wikipedia.org/wiki/Executable_and_Linkable_Formathttp://www.skyfree.org/linux/references/ELF_Format.pdf
readelfSection Headers:
[Nr] Name Type Addr Off Size ES Flg Lk Inf Al
[ 0] NULL 00000000 000000 000000 00 0 0 0
[ 1] .interp PROGBITS 08048134 000134 000013 00 A 0 0 1
[ 2] .note.ABI-tag NOTE 08048148 000148 000020 00 A 0 0 4
[ 3] .hash HASH 08048168 000168 000028 04 A 4 0 4
[ 4] .dynsym DYNSYM 08048190 000190 000050 10 A 5 1 4
[ 5] .dynstr STRTAB 080481e0 0001e0 00004a 00 A 0 0 1
[ 6] .gnu.version VERSYM 0804822a 00022a 00000a 02 A 4 0 2
[ 7] .gnu.version_r VERNEED 08048234 000234 000020 00 A 5 1 4
[ 8] .rel.dyn REL 08048254 000254 000008 08 A 4 0 4
[ 9] .rel.plt REL 0804825c 00025c 000010 08 AI 4 23 4
[10] .init PROGBITS 0804826c 00026c 00002d 00 AX 0 0 4
[11] .plt PROGBITS 080482a0 0002a0 000030 04 AX 0 0 16
[12] .plt.got PROGBITS 080482d0 0002d0 000008 00 AX 0 0 8
[13] .text PROGBITS 080482e0 0002e0 000220 00 AX 0 0 16
[15] .rodata PROGBITS 0804851c 00051c 000016 00 A 0 0 4
[24] .data PROGBITS 08049710 000710 000008 00 WA 0 0 4
[25] .bss NOBITS 08049718 000718 000008 00 WA 0 0 4
https://greek0.net/elf.html
objdump$ objdump -d -m intel hello-asm
Disassembly of section .text:
08048080 <_start>:
8048080: b8 04 00 00 00 mov eax,0x4
8048085: bb 01 00 00 00 mov ebx,0x1
804808a: b9 a4 90 04 08 mov ecx,0x80490a4
804808f: ba 0e 00 00 00 mov edx,0xe
8048094: cd 80 int 0x80
8048096: b8 01 00 00 00 mov eax,0x1
804809b: bb 00 00 00 00 mov ebx,0x0
80480a0: cd 80 int 0x80
Disassembly of section .data:
080490a4 <msg>:
80490a4: 48 dec eax
80490a5: 65 6c gs ins BYTE PTR es:[edi],dx
80490a7: 6c ins BYTE PTR es:[edi],dx
80490a8: 6f outs dx,DWORD PTR ds:[esi]
80490a9: 2c 20 sub al,0x20
80490ab: 77 6f ja 804911c <_end+0x68>
80490ad: 72 6c jb 804911b <_end+0x67>
80490af: 64 21 0a and DWORD PTR fs:[edx],ecx
Builds :)hello-dbg-asm: hello.asm nasm -g -f elf hello.asm && ld hello.o -o hello-asm
hello-c-static: hello.c gcc -static hello.c -o hello-c-static
hello-asm: hello.asm nasm -f elf hello.asm && ld hello.o -o hello-asm
for1: for.c gcc for.c -o for1
for2: for.c gcc -fopt-info-vec for.c -O2 -ftree-vectorize -o for2
for3: for.c gcc -fopt-info-vec for.c -O3 -o for3
foravx: for.c gcc -O3 for.c -fopt-info-vec -mavx -o foravx# gcc -O3 for.c -fopt-info-vec -mavx2 -o foravx
Inline assembly
#define MSG "Hello, world!"
int main() {
asm(
"int $0x80\n\t"
:
: "a"(4), "b"(1), "c"(MSG), "d"(14) );
}
Inline assembly asm(
"int $0x80\n\t"
:
: "a"(4),"b"(1),"c"(MSG),"d"(14)
);
// a,b,c,d-registers (eax,ebx,ecx & edx)
asm( “assembly instructions”
: return values : assignment variables
https://gcc.gnu.org/onlinedocs/gcc/Extended-Asm.html