gpt4 book ai didi

linux - NASM 遍历字节

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:09:10 25 4
gpt4 key购买 nike

目前我正在尝试遍历缓冲区中的每个字节(从文件中读取)并比较它以查看它们是否是空白,然后将它们写入 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

最佳答案

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

关于linux - NASM 遍历字节,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9018186/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com