nasm 文件缓冲区的第二个和第三个元素始终为 0

发布于 2024-12-09 00:40:47 字数 2614 浏览 0 评论 0原文

我有一个 NASM 代码,它读取文件(文件名存储在地址变量中)并计算 CRC5。它获取文件的每个字节并通过计算例程运行它。我观察到一个奇怪的行为:
如果我在 mov [curr], ebx 之后将断点设置为每第二次和第三次迭代 curr 变量设置为 0,则所有其他迭代都会生成正确的字符。无论我打开哪个文本文件,都会发生这种情况。

SECTION .data   
table dd 0x80, 0x40, 0x20, 0x10, 0x8, 0x4, 0x2, 0x1
address dd "test.cpp", 0
crc dd 0,0,0,0,0,10
size dw 8192

section .bss
doinvert: resb 1    
buf     resb    8192
curr    resb 1


    SECTION .text       
        global main     
main:   
    mov ebx, address

        mov   eax,  5           ; open(
        mov   ecx,  0           ;   read-only mode
        int   80h               ; );

    mov     ebx,  eax       ;   file_descriptor,
        mov     eax,  3         ; read(     
        mov     ecx,  buf       ;   *buf,
        mov     edx,  size     ;   *bufsize
        int     80h             ; );
    mov [size], eax

    mov ecx, [size]
loop_outer:
    mov eax, [size]
    sub eax, ecx
    mov ebx, [buf+eax]
    and ebx, 0ffh ; filter out extra bytes
    mov [curr], ebx
    push ecx
    mov ecx, 8
    jmp loop1
near_jump:
    jmp loop_outer

loop1:      
    mov eax, 8
    sub eax, ecx
    mov ebx, [table+eax*4]
    mov eax, [curr]
    and ebx, eax
    cmp ebx, 0
    je skip
    mov ebx, 1
skip:   
    mov eax, [crc+4*4]
    xor ebx, eax
    mov [doinvert], ebx
    mov ebx, [crc+3*4]
    mov [crc+4*4], ebx
    mov ebx,  [crc+2*4]
    mov eax, [doinvert]
    xor ebx, eax
    mov [crc+3*4], ebx
    mov ebx, [crc+1*4]
    mov [crc+2*4], ebx
    mov ebx, [crc]
    mov [crc+1*4], ebx
    mov ebx, [doinvert]
    mov [crc], ebx

    loop loop1 
    pop ecx     
    loop near_jump 

    mov ebx,0       
    mov eax,1       
    int 0x80

更奇怪的是,如果我将上面的代码减少到下面的代码,则字符会正确迭代。

SECTION .data   
table dd 0x80, 0x40, 0x20, 0x10, 0x8, 0x4, 0x2, 0x1
address dd "test.cpp", 0
crc dd 0,0,0,0,0,10
size dw 8192

section .bss
doinvert: resb 1    
buf     resb    8192
curr    resb 1


    SECTION .text       
        global main     
main:   
    mov ebx, address

        mov   eax,  5           ; open(
        mov   ecx,  0           ;   read-only mode
        int   80h               ; );

    mov     ebx,  eax       ;   file_descriptor,
        mov     eax,  3         ; read(     
        mov     ecx,  buf       ;   *buf,
        mov     edx,  size     ;   *bufsize
        int     80h             ; );
    mov [size], eax

    mov ecx, [size]
loop_outer:
    mov eax, [size]
    sub eax, ecx
    mov ebx, [buf+eax]
    and ebx, 0ffh
    mov [curr], ebx

    loop loop_outer 

    mov ebx,0       
    mov eax,1       
    int 0x80

I have a NASM code which reads a file (filename stored in address variable) and computes CRC5. It takes every byte of a file and runs it through the computation routine. There is a strange behavior I'm observing:
if I set the breakpoint after mov [curr], ebx every 2nd and 3rd iteration curr variable set to 0, all other iterations produce correct chars. This happens no matter that text file I'm opening.

SECTION .data   
table dd 0x80, 0x40, 0x20, 0x10, 0x8, 0x4, 0x2, 0x1
address dd "test.cpp", 0
crc dd 0,0,0,0,0,10
size dw 8192

section .bss
doinvert: resb 1    
buf     resb    8192
curr    resb 1


    SECTION .text       
        global main     
main:   
    mov ebx, address

        mov   eax,  5           ; open(
        mov   ecx,  0           ;   read-only mode
        int   80h               ; );

    mov     ebx,  eax       ;   file_descriptor,
        mov     eax,  3         ; read(     
        mov     ecx,  buf       ;   *buf,
        mov     edx,  size     ;   *bufsize
        int     80h             ; );
    mov [size], eax

    mov ecx, [size]
loop_outer:
    mov eax, [size]
    sub eax, ecx
    mov ebx, [buf+eax]
    and ebx, 0ffh ; filter out extra bytes
    mov [curr], ebx
    push ecx
    mov ecx, 8
    jmp loop1
near_jump:
    jmp loop_outer

loop1:      
    mov eax, 8
    sub eax, ecx
    mov ebx, [table+eax*4]
    mov eax, [curr]
    and ebx, eax
    cmp ebx, 0
    je skip
    mov ebx, 1
skip:   
    mov eax, [crc+4*4]
    xor ebx, eax
    mov [doinvert], ebx
    mov ebx, [crc+3*4]
    mov [crc+4*4], ebx
    mov ebx,  [crc+2*4]
    mov eax, [doinvert]
    xor ebx, eax
    mov [crc+3*4], ebx
    mov ebx, [crc+1*4]
    mov [crc+2*4], ebx
    mov ebx, [crc]
    mov [crc+1*4], ebx
    mov ebx, [doinvert]
    mov [crc], ebx

    loop loop1 
    pop ecx     
    loop near_jump 

    mov ebx,0       
    mov eax,1       
    int 0x80

Even more strange is that if I reduce the above code to the one below chars are iterated through correctly.

SECTION .data   
table dd 0x80, 0x40, 0x20, 0x10, 0x8, 0x4, 0x2, 0x1
address dd "test.cpp", 0
crc dd 0,0,0,0,0,10
size dw 8192

section .bss
doinvert: resb 1    
buf     resb    8192
curr    resb 1


    SECTION .text       
        global main     
main:   
    mov ebx, address

        mov   eax,  5           ; open(
        mov   ecx,  0           ;   read-only mode
        int   80h               ; );

    mov     ebx,  eax       ;   file_descriptor,
        mov     eax,  3         ; read(     
        mov     ecx,  buf       ;   *buf,
        mov     edx,  size     ;   *bufsize
        int     80h             ; );
    mov [size], eax

    mov ecx, [size]
loop_outer:
    mov eax, [size]
    sub eax, ecx
    mov ebx, [buf+eax]
    and ebx, 0ffh
    mov [curr], ebx

    loop loop_outer 

    mov ebx,0       
    mov eax,1       
    int 0x80

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

乙白 2024-12-16 00:40:47

当然...

您正在使用 dd 声明 address dd "test.cpp", 0 而不是 db (或 dw如果您使用的是widechar)。

Sure...

You are using dd declaration for address dd "test.cpp", 0 instead db (or dw if you are using widechar).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文