[英]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.我试图找到数组中无符号元素的总和,如果在存储位 1 中,则字节反转。 Test data: 126 (01111110) 8 times and 254 (11111110) will be 1 after invert and answer should be 126*8+1=1135.
测试数据:126(01111110)8次,254(11111110)反转后为1,答案应为126*8+1=1135。 but my answer is 3389849841. warning: move to read-only section ".text" warning: DT_TEXTREL is created in PIE
但我的答案是 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 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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.