【发布时间】:2022-12-05 04:25:34
【问题描述】:
我正在尝试使用以下代码读取带有程序集的 png 文件:
SECTION .bss ; Section containing uninitialized data
InBufLen: equ 3
InBuf: resb InBufLen
SECTION .text ; Section containing code
global _start ; Linker needs this to find the entry point!
_start:
call read
read: ; read chunk from stdin to InBuf
mov rax, 0 ; sys_read
mov rdi, 0 ; file descriptor: stdin
mov rsi, InBuf ; destination buffer
mov rdx, InBufLen ; maximum # of bytes to read
syscall
; check number of bytes read
cmp rax, 0 ; did we receive any bytes?
je exit; if not: exit the program
xor r10, r10
mov r10, rax ; save # of bytes read
xor r11, r11
xor rax, rax
process:
mov eax, [InBuf + r11]
xxd 说我的代码应该读出 0x89504e 作为前 3 个字节,但是在 gdb 中调试时,它说前三个字节是 0x4e5089。我想我读错了数据。
【问题讨论】:
-
您如何显示
gdb中的字节?看起来它可能将它们解释为小端格式的数值。 -
x86 是小端; EAX 值为
0x004e5089表示内存中的字节为89 50 4e 00。高字节恰好为零,因为 BSS 中没有其他内容在它之后,但通常你不应该读到你保留的东西的末尾,除非你知道接下来会发生什么并且准备忽略那个高位垃圾。 -
此外,您不需要在写入之前对寄存器进行异或归零。
mov r10d, eax复制尺寸就好了。您也不需要在mov eax, [InBuf + r11]之前将 RAX 归零;将 EAX 零扩展写入 RAX。