My studies of writing x86 assembly code.
Example:
nasm -f elf32 example01.asm -o example01.o
ld -m elf_i386 example01.o -o example01
./example01
(execute result)
Steps Overview
-
nasm -f elf32 fileName.asm -o fileName.o
- The elf32 flag tells nasm to build a 32bit elf object file
- elf = executable and linking format (used by linux)
-
ld -m elf_i386 fileName.o -o fileName
- This is GNU's Linker, called 'ld'.
- ld combines a number of object and archive files, relocates their data and ties up symbol references. Usually the last step in compiling a program is to run ld.
- Basically
- Build executable from the object file
- The elf_i386 flag specifies that it is an x86 program
- This is GNU's Linker, called 'ld'.
- http://spike.scu.edu.au/~barry/interrupts.html#ah01
- Overview of all DOS interrupts
- https://docs.microsoft.com/en-us/windows-hardware/drivers/debugger/x64-architecture
- Lists the various registers available for 64, 32, 16, and 8 bit operations.
- https://softwareengineering.stackexchange.com/questions/379683/registers-and-stacks-in-nasm
- Answer goes into detail about registers, conventions, etc.
- https://en.wikipedia.org/wiki/X86#32-bit
- Visual representation of the registers.
mov, add, sub, mul, div, int
mov eax, 123
(Mov/Assign 123 to register eax)add eax, 321
(Add 321 to eax register's value)sub eax, 100
(Subtract 100 from eax register)mul ebx
(Multiply register into eax)- NOTE: Different than others. Multiplication is always applied to the
eax
register, so in this case,mul ebx
would result in (eax * ebx).
- NOTE: Different than others. Multiplication is always applied to the
div ebx
(Divide register into eax)- NOTE: Different than others. Division is also always applied to the
eax
register, so in this case,div ebx
would result in (eax / ebx).
- NOTE: Different than others. Division is also always applied to the
int 0x80
(Interrupt)- NOTE: On Linux,
0x80
results in a system_call interrupt.
- NOTE: On Linux,
echo $?
- output the exit status of the last command
General Purpose Registers
- eax
- Accumulator
- ebx
- Base Index (for use w/ arrays)
- ecx
- Counter (for use with loops and strings)
- edx
- Extend the precision of the accumulator (e.g. combine 32-bit EAX and EDX for 64-bit integer operations in 32-bit code)
- esi
- Source index for string operations.
- edi
- Destination index for string operations.
- ebp
- Stack base pointer for holding the address of the current stack frame.
- esp
- Stack pointer for top address of the stack.
- r8d
- r9d
- r10d
- r11d
- r13d
- r14d
- r15d
EIP
(Instruction Pointer)- Location of execution.
- Not like a register.
- Changed by
jmp
operations.