繁体   English   中英

汇编程序计数和不正确

[英]Assembler program counts sum incorrectly

我试图找到数组中无符号元素的总和,如果在存储位 1 中,则字节反转。 测试数据:126(01111110)8次,254(11111110)反转后为1,答案应为126*8+1=1135。 但我的答案是 3389849841。警告:移至只读部分“.text”警告:DT_TEXTREL 是在 PIE 中创建的

; nasm -felf32 test.asm && gcc -m32 test.o && ./a.out
section .data
        msg db 'Sum: %u', 10, 0
        sum_len equ $-msg

        nextline db 10

        arr db 0, 254, 126, 126, 126, 126, 126, 126, 126, 126
        arr_len equ 10

%macro print 2
        mov eax, 4
        mov ebx, 1
        mov ecx, %1
        mov edx, %2
        int 80h
%endmacro

section .text
        global main
        extern printf

main:

        mov esi, arr
        mov ecx, arr_len

        xor ebx, ebx

loop:
        mov al, byte[esi]
        test al, 10000000b
        jz summary

        not al

summary:
        add bl, al

        add esi, 1
        dec ecx
        jnz loop

        push bx
        push msg
        call printf

exit:
        mov eax, 01
        mov ebx, 0
        int 80h

UPD

section .data
        msg db 'Sum: %u', 10, 0
        sum_len equ $-msg

        nextline db 10

        arr dd 0, 254, 126, 126, 126, 126, 126, 126, 126, 126
        arr_len equ 10

%macro print 2
        mov eax, 4
        mov ebx, 1
        mov ecx, %1
        mov edx, %2
        int 80h
%endmacro

section .text
        global main
        extern printf

main:

        mov esi, arr
        mov ecx, arr_len

        xor ebx, ebx

loop:
        mov eax, dword[esi]
        test eax, 10000000b
        jz summary

        not al

summary:
        add ebx, eax

        add esi, 4
        dec ecx
        jnz loop

        push ebx
        push msg
        call printf

exit:
        mov eax, 01
        mov ebx, 0
        int 80h

谢谢大家的帮助这里是一个工作程序

; nasm -felf32 test.asm && gcc -m32 -no-pie test.o && ./a.out
section .data
        msg db 'Sum: %u', 10, 0
        sum_len equ $-msg

        nextline db 10

        arr dd 0, 254, 126, 126, 126, 126, 126, 126, 126, 126
        arr_len equ 10

%macro print 2
        mov eax, 4
        mov ebx, 1
        mov ecx, %1
        mov edx, %2
        int 80h
%endmacro

section .text
        global main
        extern printf

main:

        mov esi, arr
        mov ecx, arr_len

        xor ebx, ebx

loop:
        mov eax, dword[esi]
        test eax, 10000000b
        jz summary

        not al

summary:
        add ebx, eax

        add esi, 4
        dec ecx
        cmp ecx, 0
        jne loop

        push ebx
        push msg
        call printf

exit:
        mov eax, 01
        mov ebx, 0
        int 80h

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM