【发布时间】:2021-01-15 00:27:20
【问题描述】:
我试图在汇编中打印一个字符串,而不必将我的数据存储到一个变量中。
什么有效:
global _main
extern _printf
section .data:
message: db 'AAAA'
section .text
_main:
push message
call _printf
add esp, 4
ret
现在,我想要的是直接将AAAA 推入堆栈并调用printf 进行打印,而无需将其存储在message 中。
我花了好几个小时用不同的方法尝试自己做,但到目前为止都失败了。
试过了:
方法#1:
push 41414141h
call _printf
方法#2:
mov esi, 41414141h
push esi
call _printf
方法#3:
mov esi, 41414141h
mov edi, 7325 ; '%s'
call _printf
; or storing 'AAAA' in EDI and storing '%s' in ESI
方法#4:
mov esi, 41414141h
mov edi, 7325 ; '%s'
push esi
push edi
call _printf
; or storing 'AAAA' in EDI and storing '%s' in ESI
方法#5:
mov esi, 41414141h
mov edi, 7325 ; '%s'
push esi
xor eax, eax
push eax ; just push a null byte to the stack
push edi
call _printf
; or storing 'AAAA' in EDI and storing '%s' in ESI
还有很多我都不记得了。
那么,打印存储在寄存器中的值的正确方法是什么。
如果也可以在下面回答,我将不胜感激。
在调查了第一次尝试的工作原理之后。我意识到,数据或AAAA 存储在程序的堆栈中,然后将数据的地址推送到ESP。
不幸的是,我不知道如何将数据推送到程序的堆栈\.data 部分,或者如果这甚至可能,以及如何将该部分的地址推送到 ESP。
【问题讨论】:
-
push 0; push 41414141h; push esp; call _printf请注意,您需要将字符串归零。您还需要删除放在堆栈上的东西。 -
@Jester,你做得更好,效果很好:)
-
您的数据部分版本仅适用,因为它后面恰好有一些
0填充。您应该使用db 'AAAA', 0来制作 C 字符串,或者使用fwrite和指针,长度,这样您就不需要终止符。 (另外,通常你会将只读常量数据放入section .rodata)