简体   繁体   中英

Assembler program counts sum incorrectly

I'm trying to find the sum of the unsigned elements in the array and if in stash bit 1, then byte invert. Test data: 126 (01111110) 8 times and 254 (11111110) will be 1 after invert and answer should be 126*8+1=1135. but my answer is 3389849841. warning: move to read-only section ".text" warning: DT_TEXTREL is created in 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

thank you all for your help here is a working program

; 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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