如何在程序集x86_64 NASM UBUNTU

时间:2019-03-13 16:14:04

标签: assembly nasm

我想从二进制文件中读取数字。我不知道文件的大小,我想知道如何一次读取一个数字直到EOF。这是我的代码:

section .bss
fd_in: resb 1
info: resb 4
section .text
global _start

_start:
mov r9d, 0   ; register to store sum of numbers
mov rax, 2 ; using in sys_open
sub rsp, 8
mov rdi, [rsp+24] ; argument from command line
mov rsi, 0
mov rdx, 0777
syscall ; opening file
mov [fd_in], rax ; storing result
file:
mov rax, 0 ; using in sys_read
mov rdi, [fd_in]
mov rsi, info
mov rdx, 4 ; reading one 32 bit number at a time
syscall
mov r8d, [info] ; storing result
bswap r8d ; changing endian-ness
add r9d, r8d ; storing sum of results
cmp eax, 0 ; checking for eof
jne file ; if not eof jump to reading again
mov rax, 3 ; closing file
mov rdi, [fd_in]
syscall
mov rax, 60 ; exiting program
mov rdi, 0
syscall

它与第一个数字一起使用时效果很好,但随后不断读取相同的第一个数字。我知道为什么,我们只在从文件读取时使用文件描述符,并且总是从头开始。我正在寻找一种从文件中一次读取所有数字的方法。

我唯一想到的方法是先读取4个字节,然后如果我们成功读取8个字节,最后4个是新的数字,以此类推,但看来效率很低。还有另一种方法吗?

0 个答案:

没有答案