繁体   English   中英

用汇编语言打印数组元素

[英]print array element in assembly language

我在nasm程序集中编写了以下代码,以便在scree上打印数组元素。在此代码中没有编译错误,但我在屏幕上得到了垃圾值。

section .data
num1: dd 10, 20, 30, 40, 50, 10, 20, 30, 40, 50,300
total: dd 0
msg :  dd "Value=%d",10,0

    section .text
        extern _printf
        global _main
    _main:
        push ebp
        mov ebp,esp
        mov ebx,num1 ;point bx to first number
        mov ecx,11     ;load count of numbers in ecx
        mov eax,0       
    loop:
        mov eax,[ebx]

        push msg
        call _printf



        add ebx,4
        sub ecx,1
        jnz loop

        mov esp,ebp
        pop ebp

        ret

section .data
num1: dd 10, 20, 30, 40, 50, 10, 20, 30, 40, 50,300
total: dd 0
msg :  dd "Value=%d",10,0

    section .text
        extern _printf
        global _main
    _main:
        push ebp
        mov ebp,esp
    mov eax,10
        mov ebx,num1 ;point bx to first number
        mov ecx,0 ;load 0

    loop:

    ;store the value because external function like printf modify the value
    push ebx
    push eax
    push ecx

        push DWORD [ebx]

        push msg
        call _printf
    add esp,8

    ;restore thses values
    pop ecx
    pop eax
    pop ebx
    inc ecx
    add ebx,4
    cmp ecx,eax
    jne loop



        mov esp,ebp
        pop ebp

        ret
  1. 显然,您想将两个参数传递给printf。 然后,您必须同时推送它们(您似乎认为其中之一是在EAX中传递的,但这不是事实)。
  2. C函数对ECX来说是免费的,因此您应该保存和恢复它(现在不使用其他保存在调用方中的寄存器,但是现在该阅读有关X86调用约定的更多信息了)。

我已经解决了我的问题,所以我在这里为其他人发布

section .data
num1: dd 10, 20, 30, 40, 50, 10, 20, 30, 40, 50,300
total: dd 0
msg :  dd "Value=%d",10,0

    section .text
        extern _printf
        global _main
    _main:
        push ebp
        mov ebp,esp
    mov eax,10
        mov ebx,num1 ;point bx to first number
        mov ecx,0 ;load 0

    loop:

    ;store the value because external function like printf modify the value
    push ebx
    push eax
    push ecx

        push DWORD [ebx]

        push msg
        call _printf
    add esp,8

    ;restore thses values
    pop ecx
    pop eax
    pop ebx
    inc ecx
    add ebx,4
    cmp ecx,eax
    jne loop



        mov esp,ebp
        pop ebp

        ret

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM