【发布时间】:2020-03-04 13:56:53
【问题描述】:
我想弄清楚如何打印出数组的行和列。该程序询问有多少行和列,并根据用户输入显示“为 [0][0] 输入一个数字”“为 [0][1] 输入一个数字”等。
这是我目前所写的:
%include "io.inc"
SECTION .data ; Data section, initialized variables
num_rows db "How many rows?: ",0
num_col db "How many columns?: ",0
prompt db "Enter a number for [%d][%d]:",10,0
sum db "The sum is: ",10,0
number db "%d",10,0
rows times 4 dd "%d",0
col times 4 dd "%d",0
arrayLen dd 9 ; length of array
;size equ rows*col
formatin db "%d", 0
section .bss
array resd 6; this is a test array for testing purposes
SECTION .text ; Code section.
global CMAIN ; the standard gcc entry point
extern printf ,scanf
CMAIN: ; the program label for the entry point
;-----Ask for the number of rows and display
push num_rows
call printf
add esp,4 ;remove the parameter
push rows ;address of rows
push formatin ;arguments are right to left
call scanf
add esp,8
;move the values into the registers
mov ebp,[rows]
push ebp
push number
call printf
add esp,8
;----Ask for the number of cols and display
push num_col
call printf
add esp,4
push col
push formatin
call scanf
add esp,8
;move the values into the registers
mov ebx, [col]
push ebx
push number
call printf
add esp,8
mov ebp,array
push ecx
push number
call printf
add esp,8
mov ecx,0
xor ebp,ebp
outerLoop:
mov edx,ecx
push ecx
mov ecx,0
inner:
push ecx
;output
push ecx
push edx
push prompt
call printf
add esp,12
;Get addr
push ecx
push edx
push esi
;call GetElement
add esp,12
;input
push eax
push number
call scanf
add esp,8
pop ecx
inc ecx
cmp ecx,[col]
jl inner
pop ecx
end_outer:
inc ecx
cmp ecx,[rows]
jl outerLoop
push sum
call printf
add esp,4
xor ebp,ebp <- My professor told me never to use this
ret
;GetElement: THIS WHOLE SUBPROGRAM IS COMMENTED
; mov ebx,[ebp+8] ;addr of array
; mov ecx,[ebp+12] ;row
; mov esi,[ebp+16] ;col
; mov eax,ecx
; mul dword [col]
; add eax,esi
; imul eax,4
; add eax,ebx
; leave
; ret
当我运行代码时,索引 [rows][cols] 无法正确打印。有人可以指导我吗?
【问题讨论】: