Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Linux 字节上的NASM循环_Linux_Assembly_X86_Nasm - Fatal编程技术网

Linux 字节上的NASM循环

Linux 字节上的NASM循环,linux,assembly,x86,nasm,Linux,Assembly,X86,Nasm,目前,我正在尝试循环缓冲区中的每一个字节(从文件读取),并对其进行比较,以确定其中是否有空白,然后将其写入标准输出。出于某种原因,该程序编译和运行良好,但产生零输出 section .data bufsize dw 1024 section .bss buf resb 1024 section .text global _start _start: ; open the file provided form cli in read mode

目前,我正在尝试循环缓冲区中的每一个字节(从文件读取),并对其进行比较,以确定其中是否有空白,然后将其写入标准输出。出于某种原因,该程序编译和运行良好,但产生零输出

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

主要的问题是我没有给出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

0x20不是“空白”,它实际上只是空白,即空白的子集。不过,这可能是吹毛求疵愚蠢的问题,但你确定你的文件中有一些
0x20
?那么这是32位linux还是BSD?所以你想打印字符直到找到一个空格,然后停止?你能提供一个测试输入文件吗?我不使用NASM,但大多数系统调用看起来都正常,即使您没有检查它们的返回代码。无论出于何种原因,这些打开/读取/写入调用都可能失败。即使它工作得很好,如果输入文件的开头有两个空格,你也不会产生任何输出(尽管它只打印一个空格)。我通过GDB编译并运行它,它产生了一个分段错误。我会进行一些错误检查,然后使用调试器检查代码。这仍然包含bug。例如,“bufsize dw”创建一个16位的值,但您将其读取为32位的值。应将其更改为“bufsize dd”或“movzx edx,word[bufsize]”。也没有错误处理(例如,如果“open()”失败怎么办?)。我还想使用(相当于)“put()”或“write()”作为输出,而不是为每个字符调用内核。更改代码以适应除错误处理之外的建议。