[英]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.