gpt4 book ai didi

linux - 在 nasm 汇编 intel x86 中获取数字而不是 Ascii

转载 作者:太空宇宙 更新时间:2023-11-04 05:02:02 24 4
gpt4 key购买 nike

我正在尝试学习汇编的基础知识,但无法理解如何显示存储在内存中的结果。

section .data
num1 db 1,2,3,4,5
num2 db 1,2,3,4,5

output: db 'The dot product is "'
outputLen1 : equ $-output
output2: db '" in Hex!', 10
output2Len : equ $-output2


section .bss
dotProd resw 1 ; store dot product in here

section .text
global _start

_start:
mov eax, 0
mov ecx, 5
mov edi, 0
mov esi, 0
looper: mov ax, [edi + num1]
mov dx, [esi + num2]
mul dx
add [dotProd], ax
cmp cx, 1
je printOutput
inc edi
inc esi
dec cx
jmp looper ; go back to looper

printOutput:

mov eax,4 ; The system call for write (sys_write)
mov ebx,1 ; File descriptor 1 - standard output
mov ecx, output ;
mov edx, outputLen1 ;
int 80h ; Call the kernel

mov eax, 4
mov ebx, 1
mov ecx, dotProd,
mov edx, 1
int 80h

mov eax, 4
mov ebx, 1
mov ecx, output2,
mov edx, output2Len
int 80h

jmp done
done:
mov eax,1 ; The system call for exit (sys_exit)
mov ebx,0 ; Exit with return code of 0 (no error)
int 80h

我想做的是获取两个数字列表的点积并将其显示在屏幕上。但是,我不断收到随机字母,我认为它们是实际十进制值的十六进制表示形式。如何将其转换为十进制?当前显示的值是 7,它应该是 55 的等效 ASCII 字符,在本例中是两个数字列表的点积。

最佳答案

  • esiedi 必须增加,以便它指向数组的下一个元素。(在此特定示例中,只需其中一个就足够了)。
  • mun1num2 声明为 dd,而不是 db(请参阅 here )。另外,你必须有打印数字的方法。(参见 thisthis )。

下面是使用 printf 的完整代码。

;file_name:test.asm
;assemble and link with:
;nasm -f elf test.asm && gcc -m32 -o test test.o
extern printf
%macro push_reg 0
push eax
push ebx
push ecx
push edx
%endmacro
%macro pop_reg 0
pop edx
pop ecx
pop ebx
pop eax
%endmacro
section .data
num1: dd 1,2,3,4,5
num2: dd 1,2,3,4,5
msg: db "Dot product is %d",10,0

section .bss
dotProd resd 1 ; store dot product in here

section .text
global main

main:
mov eax, 0
mov ecx, 5
mov edx, 0
mov esi, 0
mov dword[dotProd], 0h
looper: mov eax, dword[esi + num1]
mov edx, dword[esi + num2]
mul edx

add [dotProd], eax

cmp cx, 1
je printOutput
add esi,4
dec cx

jmp looper ; go back to looper

printOutput:

push_reg
push dword[dotProd]
push dword msg
call printf
add esp,8
pop_reg

jmp done
done:
mov eax,1 ; The system call for exit (sys_exit)
mov ebx,0 ; Exit with return code of 0 (no error)
int 80h

关于linux - 在 nasm 汇编 intel x86 中获取数字而不是 Ascii,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29830045/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com