printf打印垃圾数量

时间:2016-02-23 12:35:23

标签: assembly x86 printf

我正在挠头八小时,只是为了在装配中打印两个数字的总和。

这是我的简单代码:

.386
.model flat,stdcall
option casemap:none

.data
msg     dd  32h
str1 db "Hello",0
fmt    db "Sum: %X",0

.data?
retvalue dd ?

.code
include windows.inc
include user32.inc
includelib user32.lib
include kernel32.inc
includelib kernel32.lib
includelib MSVCRT
extrn printf:near
extrn exit:near

public main
main proc


         mov eax, 17h       ; 23
         mov ecx, 1Eh     ; 30
         add eax, ecx
         mov retvalue,eax
         ;push offset retvalue
         push offset msg
         ;push offset str1
         push offset fmt
         call printf 
         push 0
         call exit

main endp

end main

问题是最终的printf会打印出垃圾编号:

输出结果为:Sum: 403000

预期输出:Sum: 53

编辑:

当我尝试执行以下操作时,它无法正常工作

  push retvalue
  ;push  msg
  push str1
  push  offset fmt
  call printf 

我改变了我的fmt

fmt db "%d & %s",0

错误:

  

(32):错误A2070:无效的指令操作数

EDIT2:

在通过str1的偏移之后,程序仍然编译并链接,但似乎无法运行。

错误:

myapp.exe has stopped responding

代码:

fmt db "%d & %s",0

  push retvalue
  ;push  msg
  push offset str1
  push  offset fmt
  call printf 

1 个答案:

答案 0 :(得分:1)

显示的代码打印msg的地址,因为您push offset msgprintf期望打印值,而不是指向它的指针。因此,这应该有效:

     mov eax, 17h       ; 23
     mov ecx, 1Eh     ; 30
     add eax, ecx
     push eax
     push offset fmt
     call printf 
相关问题