ASM
参考 x86assembly, 汇编语言格式有 NASM、MASM、gas 等。
安装
$ sudo apt install nasm
$ sudo apt install gcc
gas
gcc
$ gcc -v
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/lib/gcc/i686-linux-gnu/9/lto-wrapper
Target: i686-linux-gnu
Thread model: posix
gcc version 9.4.0 (Ubuntu 9.4.0-1ubuntu1~20.04.1)
参考 gas example
.global _main
.text
_main:
# write(1, message, 13)
mov $1, %eax # system call 1 is write
mov $1, %edi # file handle 1 is stdout
mov $message, %esi # address of string to output
mov $13, %edx # number of bytes
syscall # invoke operating system to do the write
# exit(0)
mov $60, %eax # system call 60 is exit
xor %edi, %edi # we want return code 0
syscall # invoke operating system to exit
message:
.ascii "Hello, world\n"
NASM
https://www.tutorialspoint.com/assembly_programming/assembly_basic_syntax.htm
Hello
新建 hello.asm
section .text
global _start ;must be declared for linker (ld)
_start: ;tells linker 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 ;system call number (sys_exit)
int 0x80 ;call kernel
section .data
msg db 'Hello, world!', 0xa ;string to be printed
len equ $ - msg ;length of the string
# 编译
$ nasm -f elf hello.asm
# 连接
$ ld hello.o
# $ ld -m elf_i386 -s -o dt dt.o
# 运行
$ ./a.out
DT
section .text
global _start
_start:
SGDT [gdtcopy]
SLDT [ldtcopy]
section .data
gdtcopy times 16 db 'x'
ldtcopy times 16 db 'x'
$ nasm -f elf dt.asm
$ ld dt.o
$ gdb ./a.out
(gdb) layout asm
(gdb) starti
(gdb) si
(gdb) x/32x 0x804a000
0xff 0x00 0x00 0x40 0x7c 0xf6
References
- https://cs.lmu.edu/~ray/notes/x86assembly/