将一个字符串按逆序输出(堆栈实现)
include Irvine32.inc
.data
str1 byte "hello world!",0 ;此处str1是字符串
size1 equ $-str1-1 ;由于是字符串,所以要减去后缀0
.code
main proc
mov ecx,size1
xor esi,esi
;压入堆栈
again1:
movzx eax,str1[esi]
push eax
inc esi
loop again1
mov ecx,size1
xor esi,esi
;逆序弹出
again2:
pop eax
mov str1[esi],al
inc esi
loop again2
;输出字符串
mov edx,offset str1
call writestring
exit
main endp
end main
将数组元素逆序输出(堆栈实现)
include Irvine32.inc
.data
num byte 1,2,3,4,5,6,7
num_size equ $-num
.code
main proc
;压入堆栈
mov ecx ,num_size
xor esi,esi
again1:
mov eax,dword ptr num[esi]
push eax
inc esi
loop again1
;弹出堆栈,将逆序后的数存入原数组
mov ecx, num_size
xor esi,esi
again2:
pop eax
mov num[esi],al ;切记al不要改为eax
inc esi
loop again2
;输出逆序结果
mov ecx ,num_size
xor esi,esi
again3:
movzx eax,num[esi]
call writedec
call crlf
inc esi
loop again3
;逆序结果为:7 6 5 4 3 2 1
exit
main endp
end main