汇编程序计算总和不正确
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
我试图找到数组中无符号元素的总和,如果在存储位 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