linux - NASM 为注册 bug 增加值(value)

标签 linux assembly x86 nasm

我正在尝试将几个像素添加在一起,以便在 NASM 中进行模糊过滤。我成功添加了三个像素,其值为 00 + d3 + d8 (0 + 211 + 216)。当我尝试再添加一个值为 0 的像素时,程序无法打印变量 blurr 的值。

更新:
似乎添加到变量 sum 最多可以完成三次,因为如果我注释掉另一个 add,该值将打印在我的输出文件中。

blurrTopRow:
    ;from 0 - 251 there will be no pixels above the active pixel

    ;set ah to 0 to be sure that no other values changes the byte
    ;save byte in al, ax should be [0000](ah) value(al)
    mov ah, byte 0
    mov al, byte [info + 0]

    ;store sum all pixels in sum, divition will be done here
    add [sum], ax

    ;add pixel beside it (1)
    ;mov ah, byte 0
    mov al, byte [info + 1]

    ;add the value to sum
    ;add [sum], ax    If i add this value, the program stops working

    ;add the pixels below the first pixel
    ;move data to the first 8-bits
    mov ah, 0
    mov al, byte [info + 251]
    add [sum], ax

    ;set the last 8-bits in the 16-bit register (ax) to 0
    ;to avoid messing up the value
    mov ah, 0
    mov al, byte [info + 252]
    add [sum], ax

    ;devide the digit with 4
    mov eax, 0 
    mov ax, [sum]

    mov ebp, 4 
    mov edx, 0
    idiv ebp

    mov [blurr], al

    ret

我相信这是由于一些我还不理解的字节错误或有效寻址造成的。如果你想查看我的所有代码,可以在 pastebin 中找到。

目前,我非常困惑为什么在我的 sum 中添加 0 会破坏程序,尤其是当我已经在上面的代码中完成此操作时。

最佳
塞布

最佳答案

我有一个想法 - 我不确定它是否正确:

在你的程序中你调用了“open”两次。有一次你注释掉了 mov ecx, ...;另一次,ecx 寄存器根本没有被设置过:

openFileIn:
    mov eax, 5
    mov ebx, fileName
    ; <-- Here you are trusting Linux that ecx=0 on program start
    ;     This is not guaranteed;
    ;     it may change in future Linux versions!
    mov edx, 0777
    int 0x80
    mov [fd_in], eax
    ret

openFileOut:
    mov eax, 5
    mov ebx, outName
    ;mov ecx, 1   <-- why did you comment this out?
    ;                 Maybe "1" is not the correct value!
    mov edx, 0777
    int 0x80

在另一行中,您将一些地址写入 ecx 寄存器:

readFromFileIn:
    mov eax, 3
    mov ebx, [fd_in]
    mov ecx, info       ; <-- Here
    mov edx, IMAGE_SIZE
    int 0x80
    ret

当您向代码中添加指令时,程序中元素的地址可能会发生变化 - 包括 info 的地址。

我怀疑,如果没有附加指令,info 的地址偶然是“open”系统调用的有效参数,而在插入指令后,该地址就不是不再是“open”的有效参数。

您可以通过使用 strace 工具运行程序的两个变体来测试这一点,该工具会显示使用 wich 参数调用了哪些系统调用。

关于linux - NASM 为注册 bug 增加值(value),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45999476/

相关文章:

c - Unix 服务器客户端连接 : Connection refused

linux - Linux 中的目录大小。性能问题

caching - 为什么需要单独的 icache 和 dcache

assembly - x86中的BEXTR指令是如何工作的

assembly - MMX 是否确实支持 PADDD 指令,尽管 Intel 手册中缺少该指令?

linux - 对文件夹中的每个文件运行单个命令 - LINUX

php - 在kali linux的LAMP上安装zf2?

linux - x86_64 Linux syscall参数

c++ - 内联 asm 缓冲区循环

c - execve x86 - 段错误