在一篇入门的基础上,我们来实现字符串的逆序打印。
上次的代码如下。
section .data
msg db "hello,world",0xA
len equ $-msg
section .text
global _start
_start:
mov eax,4
mov ebx,1
mov ecx,msg
mov edx,len
int 0x80;触发系统调用
mov eax,1
xor ebx,ebx
int 0x80
上次通过实现可以打印出来hello,world。本次将实现打印dlrow,olleh。实现代码如下所示。
section .data
msg db "hello,world",0xA
len equ $-msg
section .text
global _start
_start:
mov eax,4
mov ebx,1
mov ecx,msg
mov edx,len
int 0x80;触发系统调用
mov ecx,len-1
mov esi,0
L1:mov al,msg[esi]
push qword rax
inc esi
loop L1
mov ecx,len-1
mov esi,0
L2:pop rax
mov msg[esi],al
inc esi
loop L2
mov eax,4
mov ebx,1
mov ecx,msg
mov edx,len
int 0x80;触发系统调用
mov eax,1
xor ebx,ebx
int 0x80
运行完的结果如下所示。
逆序的过程中借助了stack,在指令集之中可以使用push和pop来操作stack。
源码库在汇编代码库可以查看。