2014-11-04 66 views
1

我试图把参数(这将总是是一个单一的数字),它从一个字符串转换为 一个int,并打印出来在屏幕上。传递参数,并打印出来

当我运行我的程序与

./test 3 

我得到一个非常大的负值...

这是我有:

SECTION .data 
str1: db "argument = %d",0,10 

SECTION .text 
global main 
extern printf 

main: 
    enter 0,0 
    pusha 

    mov eax, dword [ebp+12] 
    add eax, 4     
    mov bl,[eax]    ; put the one bit into bl 

    sub ebx, '0'    ; subtract null terminating string to convert to int 

    push ebx 
    push str1 
    call printf    ; print the string 
    add esp, 8    

    popa 
    leave 
    ret 

回答

1
xor ebx, ebx    ; Clear ebx to zero, so the following operation will not have random values in the higher part of ebx 
mov bl,[eax]    ; put the one bit into bl 
sub ebx, '0'    ; subtract null terminating string to convert to int 
+0

我增加了行'xor ebx,ebx',但仍然没有给我正确的答案 – jean 2014-11-04 14:06:47

+0

你没有提到你在哪个操作系统上。在Windows中,您必须调用“GetCommandLine”。你为什么认为它会在[ebp + 12]?您是否使用调试器来检查您是否真正阅读了正确的值? http://www.network54.com/Forum/632471/message/1307235592/Getting+command+line+arguments+using+the+Win32+API – Devolus 2014-11-04 15:23:09