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

标签 linux assembly x86 nasm

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

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/

相关文章:

string - 交换字符串的第一个和最后一个字符会导致段错误

c - 来自 fork 进程的longjmp

c - volatile 关键字用于指针数组对象,用于经常变化的网络数据包信息容器 C

linux - 为什么我可以在未经用户许可的情况下删除文件?

gcc - 编译器总是生成汇编代码吗?

gcc - 让 GCC 优化手工 assembly

assembly - 引导加载程序堆栈配置

linux - 如何在一行中使用多个 bash 命令

linux - 每个月运行 cron 作业并且命令的结果需要在一个文件中

linux - 将 32 位程序转换为 16 位程序