x86 NASM Linux程序集中的ATOI问题

时间:2013-03-18 18:56:06

标签: linux gcc assembly x86 nasm

我不明白如何将字符串转换为整数。

这是作业,我不想要问题的答案 - (AKA正确的代码)。如果有人可以解释我做错了什么,我真的很感激! :(

提前致谢!!!

我在虚拟机上运行Ubuntu 12.04,32位。

我编译:

nasm -f elf proj2.asm

我链接:

gcc -o proj2 proj2.o

然后运行它:

./proj2

它会显示第一个数字,但在我尝试使用atoi时会给我一个分段错误。

我有一位老师想要我们:

  1. 从如下排列的文本文件中读取数字:

    4
    5
    4
    2
    9
    
  2. (每个整数前都有空格)

    根据他的指示:“确保在缓冲区中读取七(7)个字符以获得整行。这五个字符代表数字以及字符CR和LF.CR是十六进制的回车符号代码0x0D和LF是换行符,其中包含十六进制代码0x0A。“)

    我已经删除了文件中的空格,并尝试以这种方式阅读,但它没有帮助。

    要将int读入堆栈中的数组,最大内存数为250.但这不是问题:/

    以下是我的代码。

        BUFFERSIZE equ 10
    
    section .data
        file_name: db "/home/r/Documents/CS/project2/source/indata.txt", 0x00
        file_mode: db "r", 0x00
        output: db "%i",0xa
        test: db "hello world",10
        format: db "%u"
        numToRead: db 1
        temp: db "hi"
        num:db "1",0,0
    section .bss
        fd:    resd 4
        length:    resd 4
        buffer resb BUFFERSIZE
                                ;i was trying to use buffers and just
                                ;read through each character in the string, 
                                ;but i couldn't get it to work
    section .text 
     extern fopen
     extern  atoi
     extern printf
     extern fscanf
    
     extern fgets
     extern getc
     extern fclose
     global main
    
    main: 
                        ;setting up stack frame
        push    ebp
        mov     ebp, esp 
    
                        ;opens file, store FD to eax
        push    file_mode
        push    file_name
        call    fopen
    
                        ;save FD from eax into fd    
        push    eax
        mov     ebx,eax
        mov     [fd],ebx
    
                        ;ebx holds the file descriptor
                        ;push in reverse order
        push    ebx
        push    numToRead
        push    temp
        call    fgets    
    
        push  eax
        call printf     ;prints length (this works, i get a 4. 
                        ;Changing the number in the file changes the number displayed. 
                        ;I can also read in several lines, just can't get any ints! 
                        ;(So i can't do any comparisons or loops :/ )
    
    
                        ;i shouldn't need to push eax here, right? 
                        ;It's already at the top of the stack from the printf
        ;pop  eax
        ;push eax
        call atoi
                            ;calling atoi gives me a segmentation fault error
        push eax
        call printf
    
        mov esp,ebp
        pop ebp 
        ret 
    

    编辑: 有趣的是,我可以打电话给atoi。那是我试着

    的时候
    push eax
    call atoi 
    push eax
    call printf 
    

    我得到了分段错误。

2 个答案:

答案 0 :(得分:2)

除非我无法在手机上看到它,但是在通话结束后你不能平衡堆栈。那些c函数不是stdcall所以你必须在每次调用后调整堆栈。我这样做:

add esp, 4 * numofpushes可能是您的seg错误的来源。

答案 1 :(得分:0)

  

编辑:有趣的是,我可以打电话给atoi。那是我试着

的时候
  push eax
  call atoi 
  push eax
  call printf 
  

我得到了分段错误。

atoi reference“成功时,该函数将转换后的整数作为int值返回。”
传递任何随机整数(如4)作为以下printf的第一个参数(即format字符串指针)不太可能结束。