从汇编GAS编译器中的文件中逐字符读取数据

时间:2016-06-20 20:21:47

标签: linux assembly x86

我正在用GAS编译器和AT& T语法编写汇编程序。

我有两个文件:一个要读取,另一个要写入。 从终端i以这种方式启动程序:./myprogram inputfile.txt outputfile.txt

现在我想逐个字符地阅读输入数据,这就是我尝试这样做的方式:

.section .data
  buff_size: .long 1

.section .bss
  .lcomm buff, 18

.section  .text              # declaring our .text segment
  .globl _start              # telling where program execution should start

_start:

    popl %eax       # Get the number of arguments
    popl %ebx       # Get the program name
    popl %ebx       # Get the first actual argument - file to read

    # open the file
    movl $5, %eax       # open
    movl $0, %ecx       # read-only mode
    int $0x80       

    # read the file

    movl $0, %esi

    # these 6 instructions read first character from inputfile.txt
    movl %eax, %ebx     # file_descriptor,
    movl $3, %eax       
    movl $buff, %edi    
    leal (%esi,%edi,1), %ecx    
    movl buff_size, %edx
    int $0x80

    # these 6 instructions can't read the second character from inputfile.txt and I can't understand why.
    movl $1, %esi
    movl $3, %eax       
    movl $buff, %edi    
    leal (%esi,%edi,1), %ecx    
    movl buff_size, %edx
    int $0x80


    # open the file
    popl %ebx       # Get the second actual argument - file to write
    movl $5, %eax       # open
    movl $2, %ecx       # read-only mode
    int $0x80       

    # write to STDOUT
    movl %eax, %ebx     # file_descriptor
    movl $4, %eax       
    leal buff, %ecx     
    movl buff_size, %edx    
    int $0x80       

    # exit
    movl $1, %eax       
    movl $0, %ebx       
    int $0x80       

这段代码成功读取inputfile.txt中的第一个字符,并将该字符写入outputfile.txt ...

现在我想读写第二个字符,但它并没有像我通过代码编写的评论中所建议的那样工作。

仅供参考:

操作系统:Ubuntu 14 - 64位 编译器:GAS

我的inputfile.txt看起来如此:4,3,55,15,8,9

0 个答案:

没有答案