Arrays 如何在NASM中实现2D阵列

Arrays 如何在NASM中实现2D阵列,arrays,assembly,nasm,sasm,Arrays,Assembly,Nasm,Sasm,我试图弄清楚如何打印出数组的行和列。该程序询问有多少行和列,并根据用户输入显示为[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

我试图弄清楚如何打印出数组的行和列。该程序询问有多少行和列,并根据用户输入显示为[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]无法正确打印。有人能指导我吗?

你的问题与访问数组元素无关-你的逻辑是正确的,尽管imul eax,4是个坏主意,应该用shl eax,2或lea eax,[eax*4]或lea eax,[ebx+eax*4]代替,因为数组中的偏移量不是有符号的值,避免这种乘法更快

相反;你的问题是C调用约定很糟糕。它们用大量额外的指令来操作堆栈,使代码更难读取和调试,从而污染了代码;并对其进行优化,例如使用子esp。。。要为要传递给任何子函数和mov[rsp+…]的最大参数保留空间。。。而不是推。。。在调用子函数之前设置参数是痛苦的;整个过程最终都是一个容易出错且缓慢的混乱状态,这对于汇编来说是不必要的,除非您调用的是由C编译器编译的函数

更具体地说;对于GetElement,您使用ebp作为堆栈帧,但没有将ebp设置为堆栈帧,因此当函数尝试将堆栈中的参数获取到寄存器中时,函数不会从正确的位置获取参数

要真正遵守C调用约定CDECL,它应该更像:

GetElement:
    push ebp
    mov ebp,esp            ;Set up ebp as stack frame

    push ebx               ;ebx is "callee preserved" (not "caller saved") and is modified, so it must be saved/restored
    push esi               ;esi is "callee preserved" (not "caller saved") and is modified, so it must be saved/restored

    mov ebx,[ebp+3*4+4]    ;addr of array
    mov ecx,[ebp+3*4+4+4]  ;row
    mov esi,[ebp+3*4+4+8]  ;col

    mov eax,ecx
    mul dword [col]
    add eax,esi
    lea eax,[ebx+eax*4]

    pop esi
    pop ebx

    leave
    ret
具有讽刺意味的是,对于您的代码,参数已经在寄存器中-唯一的调用方正在执行以下操作:

    push ecx         ;col
    push edx         ;row
    push esi         ;address of array
    call GetElement
    add esp,12
…这意味着,如果您忘记了C调用约定,您的GetElement可能会像这样删除10条不必要的指令:

;Inputs:
; ecx = column
; edx = row
; esi = address of array
;
;Outputs:
; eax = address of element in the array
;
;Trashed:
; edx

GetElement:
    mov eax,edx
    mul dword [col]
    add eax,ecx
    lea eax,[esi+eax*4]
    ret
    call GetElement
..调用代码可能如下所示:删除4条不必要的指令:

;Inputs:
; ecx = column
; edx = row
; esi = address of array
;
;Outputs:
; eax = address of element in the array
;
;Trashed:
; edx

GetElement:
    mov eax,edx
    mul dword [col]
    add eax,ecx
    lea eax,[esi+eax*4]
    ret
    call GetElement

如果从moveax、[col]开始,GetElement可能会更高效;imul eax,edx。无需使用速度较慢的mul,该mul需要一个或两个额外的uop将高半部写入EDX。另外,你也不会丢弃任何寄存器。