Upgrade to Pro — share decks privately, control downloads, hide ads and more …

x86_64 Assembly Language

David Bull
November 05, 2023

x86_64 Assembly Language

David Bull

November 05, 2023
Tweet

More Decks by David Bull

Other Decks in Programming

Transcript

  1. Registers rax eax ah al ax rbx ebx bh bl

    bx rcx ecx ch cl cx rdx edx dh dl dx rsi esi sil si rdi edi dil di rbp ebp bpl bp rsp esp spl sp r8 r8d r8b r8w … … … … r15 r15d r15b r15w 63 31 15 0 8 7
  2. Instructions mov eax, 0x000F ; Store the value 15 into

    the 
 ; eax register 
 
 lea rdi, [message] ; Load the address of [message] 
 ; into the rdi register 
 
 push rcx ; push the value in the rcx 
 ; register onto the top of the stack 
 
 pop rcx ; pop the value on the top of the 
 ; stack into the rcx register Data Transfer
  3. Instructions mov rax, 12 ; Load the value 12 into

    the rax register mov rcx, 5 ; Load the value 5 into the rcx register add rax, rcx ; Add the value in rcx to the value in 
 ; rax, and store the result in rax (17) 
 
 mov rax, 12 ; Load the value 5 into the rax register mov rcx, 5 ; Load the value 12 into the rcx register sub rax, rcx ; Subtract the value in rcx from the value 
 ; in rax, and store the result in rax (7) 
 
 mov rax, 7 ; Load the value 7 into the rax register inc rax ; Increment the value in rax by 1 (8) 
 
 mov rax, 7 ; Load the value 7 into the rax register dec rax ; Decrement the value in rax by 1 (6) Arithmetic
  4. Instructions cmp rax, 5 ; Compare the value in rax

    with the value 5 
 je label ; Jump to label if equal (==) 
 jne label ; Jump to label if not equal (!=) 
 jg label ; Jump to label if greater than (>) 
 jng label ; Jump to label if not greater than (!>) 
 jge label ; Jump to label if greater than or equal (>=) 
 jnge label ; Jump to label if not greater than or equal (!>=) 
 jl label ; Jump to label if less than (<) 
 jnl label ; Jump to label if not less than (!<) 
 jle label ; Jump to label if less than or equal (<=) 
 jnle label ; Jump to label if not less than or equal (!<=) 
 jz label ; Jump to label if zero (== 0) 
 jnz label ; Jump to label if not zero (!= 0) Conditions
  5. global start section .text start: mov rax, 0x02000004 mov rdi,

    1 mov rsi, message mov rdx, 13 syscall mov rax, 0x02000001 mov rdi, 0 syscall section .data message: db "Hello, World", 10 Directives Sections Labels Instructions Operands
  6. ; ---------------------------------------------------------------------------------------- ; Writes "Hello, World" to the console using

    only system calls. Runs on 64-bit macOS only. ; To assemble and run: ; ; nasm -fmacho64 hello.asm && ld hello.o -static -o hello && ./hello ; ---------------------------------------------------------------------------------------- global start section .text start: mov rax, 0x02000004 ; system call for write mov rdi, 1 ; file handle 1 is stdout mov rsi, message ; address of string to output mov rdx, 13 ; number of bytes syscall ; invoke operating system to do the write mov rax, 0x02000001 ; system call for exit mov rdi, 0 ; exit code 0 syscall ; invoke operating system to exit section .data message: db "Hello, World", 10 ; note the newline at the end
  7. #include <stdio.h> int main() { int i; for (i =

    1; i <= 10; i++) { printf("%d\n ", i); } return 0; }
  8. ; ---------------------------------------------------------------------------------------- ; Prints the numbers 1-10 using the C

    library printf function. Runs on 64-bit macOS only. ; To assemble and run: ; ; nasm -fmacho64 numbers.asm ; ld -L /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/lib -lc -o numbers numbers.o ; ./numbers ; ---------------------------------------------------------------------------------------- global _main extern _printf default rel section .text _main: mov rcx, 1 ; initialise the counter to 1 print_loop: ; Print the counter push rcx ; caller - save register lea rdi, [format] ; set 1st parameter (format) mov rsi, rcx ; set 2nd parameter (current number), call _printf ; call the C library printf function pop rcx ; restore caller-saved register ; Increment the counter and loop while <= 10 inc rcx ; increment the counter cmp rcx, 11 ; compare the counter value to 11 jne print_loop ; jump to print_loop if not equal ; Exit the program mov rax, 0 ; set the exit code ret section .data format: db "%d", 10, 0 ; note the newline at and NULL characters at the end