P
P
PRIYD2021-05-04 14:02:28
assembler
PRIYD, 2021-05-04 14:02:28

How to output a 2D array?

You need to display the two-dimensional array contained in the structure on the screen. I refer to memory for reasons that array[i][j] = (address of the first element + N*M*i + N*j), where N is the size of the element in bytes, M is the number of rows in two. array, i and j - counters in cycles (I use rcx and rdx, respectively). It seems that the register has chosen the right one, but still an error

main.asm [26]:
      mov ax, [field.grid + 8*(field.size_n*rcx + rdx)]	
processed: mov ax,[field.grid+8*(field.size_n*rcx+rdx)]
error: operand sizes do not match.
when compiling. Architecture x64, FASM preprocessor

Code:
format ELF64
public _start

struc playing_field n {
  .size_n db n
  .grid rw n*n
  .max_score db 0
}

field playing_field 4

_start:
  call exit

section ".print_grid" executable
; | input:
; rax = array
print_grid:
  xor rcx, rcx
  .first_iter:
    xor rdx, rdx
    .second_iter:
      mov ax, [field.grid + 8*(field.size_n*rcx + rdx)]	
      
      inc rdx
      cmp rdx, field.size_n
      je .next
      jmp .second_iter
    .next:
      inc rcx
      cmp rcx, field.size_n
      je .quit
      jmp .first_iter	
  .quit:
    ret

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
VitalyChaikin, 2021-05-05
@PRIYD

So he writes - operand sizes do not match
First calculate 8*(field.size_n*rcx + rdx) put in bx, and only then apply:
mov ax, [field.grid][bx]

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question