[英]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
我已经解决了我的问题,所以我在这里为其他人发布
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.