NASM循环字节

时间:2012-01-26 12:35:56

标签: linux assembly x86 nasm

目前我正在尝试遍历缓冲区中的每个字节(从文件中读取)并比较它以查看它们中的任何一个是否为空格,并将它们写入STDOUT。由于某种原因,程序编译并运行正常,但产生零输出。

section .data
   bufsize dw      1024

section .bss
   buf     resb    1024
section  .text
  global  _start

_start:
    ; open the file provided form cli in read mode
    mov edi, 0
    pop   ebx
    pop   ebx
    pop   ebx
    mov   eax,  5
    mov   ecx,  0 
    int   80h
    ; write the contents in to the buffer 'buf'
    mov     eax,  3
    mov     ebx,  eax
    mov     ecx,  buf
    mov     edx,  bufsize
    int     80h

    ; write the value at buf+edi to STDOUT 
    mov     eax,  4
    mov     ebx,  1
    mov     ecx,  [buf+edi]
    mov     edx, 1
    int     80h
    ; if not equal to whitespace, jump to the loop
    cmp byte [buf+edi], 0x20
    jne loop

loop:
    ; increment the loop counter
    add     edi, 1
    mov     eax,  4
    mov     ebx,  1
    mov     ecx,  [buf+edi]
    int     80h
    ; compare the value at buf+edi with the HEX for whitespace
    cmp byte [buf+edi], 0x20
    jne loop

; exit the program
mov   eax,  1
mov   ebx,  0 
int   80h

1 个答案:

答案 0 :(得分:1)

主要问题是我没有给出bufsize([bufsize])的地址,循环也存在一些问题。

这是固定版本,感谢大家的意见。

section .data
   bufsize dd      1024

section .bss
   buf:     resb    1024
section  .text
  global  _start

_start:
    ; open the file provided form cli in read mode
    mov edi, 0
    pop   ebx
    pop   ebx
    pop   ebx
    mov   eax,  5
    mov   ecx,  0
    int   80h
    ; write the contents in to the buffer 'buf'
    mov     eax,  3
    mov     ebx,  eax
    mov     ecx,  buf
    mov     edx,  [bufsize]
    int     80h

    ; write the value at buf+edi to STDOUT
    ; if equal to whitespace, done
loop:
    cmp byte [buf+edi], 0x20
    je done
    mov     eax,  4
    mov     ebx,  1
    lea     ecx,  [buf+edi]
    mov     edx,  1
    int     80h
    ; increment the loop counter
    add     edi, 1
    jmp loop
done:
; exit the program
mov   eax,  1
mov   ebx,  0
int   80h