Reading notes about assembly programming for future use.
Hello world
Tools
- nasm (compiler)
- ld (linker)
- gdb (debugger)
References
- https://www.cs.virginia.edu/~evans/cs216/guides/x86.html
- https://www.tutorialspoint.com/assembly_programming/assembly_basic_syntax.htm
- https://www.nayuki.io/page/a-fundamental-introduction-to-x86-assembly-programming
- https://en.wikipedia.org/wiki/X86_calling_conventions#cdecl
- https://www.mourtada.se/calling-functions-in-x86-assembly/
Code
section .text
global _start ; must be declared for linker (ld) (like main function)
_start: ; entry point
mov edx, len ; message length
mov ecx, msg ; message to write
mov ebx, 1 ; file descriptor (stdout)
mov eax, 4 ; system call number (sys_write)
int 0x80 ; call kernel
mov eax, 1 ; sys cam number (sys_exit)
int 0x80 ; call kernel
section .data
msg db 'Hello world|', 0xa
len equ $ - msg Compile
nasm -f elf64 -g -F dwarf <source.s>
# -g -F dwarf : for debugging purposesLink
ld <source.o> -o <exec_nane>
# eg. ld hw.o -o execDebug
gdb <exec_name> -tui
# optional: -tui (text user interface)Useful gdb commands:
- run (r)
- step (s)
- next (n)
- continue (c)
- breakpoint (b)
, : , - backtrace (bt)
- info registers
- layout regs
- quit (q)
Hello world
def foo
puts 'foo'
end