0

I saw answers for this question that has already been asked, but I always see that problem is that they forgot the -f win64 or are trying to use 2 memory operands instead of a register. I don't know why it's happening to me, I'm compiling correctly and not using 2 memory operands.

This program calculates the trace of a 3x3 matrix of 16-bit elements

How I compile:

nasm myprogram.asm -f win64

gcc myprogram.obj -o myprogram

My code:

global main

extern printf

section .data
  matriz dw 3,4,5
         dw 11,6,15
         dw 9,5,14
  traza dw 0
  msjTraza  db "La traza es: %hi",0
  longElemento dw 2 ;Porque cada elemento es un "word" (2 bytes)
  longFila dw 6 ;Sale de 2 * 3 (longitud de fila = longitud de elemento * cantidad de columnas)
  fila dw 1
  columna dw 1

section .bss

section .text
main:

  ;Calculo la traza
  call calcularTraza

  ;Imprimo la traza
  call imprimirTraza

  ret

calcularTraza:

  ;Cuando la fila es mayor a 3, entonces el algoritmo ya termino (ya se calculo la traza)
  cmp word[fila],3
  jg regresar

  ;Calculo la posicion del elemento de la matriz a sumar (los elementos de su diagonal principal)
  mov bx,word[fila]
  dec bx
  imul bx,word[longFila] ;En bx queda (i - 1) * longitud de fila, donde "i" es "fila"

  mov dx,word[columna]
  dec dx
  imul dx,word[longElemento] ;En dx queda (j - 1) * longitud del elemento, donde "j" es "columna"

  add bx,dx ;Quedandome en "bx" entonces la posicion del elemento al que debo ir a buscar ( (i-1) * longitudFila + (j-1) * longitudElemento )

  ;Voy a buscar el elemento en cuesiton y lo guardo en ax
  mov ax,word[matriz + bx]

  ;Le sumo el elemento a la traza
  add word[traza],ax

  ;Incremento en 1 la fila y la columna
  inc word[fila]
  inc word[columna]

  ;Loopeo
  jmp calcularTraza

regresar:
  ret

imprimirTraza:
  mov rcx,msjTraza
  mov dx,word[traza]
  sub rsp,32
  call printf
  add rsp,32

  ret
Michael Petch
  • 46,082
  • 8
  • 107
  • 198
Martin
  • 1
  • 1
  • You can't use a 16-bit register in the memory operand when doing this `mov ax,word[matriz + bx]` in 64-bit code. You have to use a 32 bit register like EBX or a 64-bit one like RBX instead of BX – Michael Petch Oct 24 '22 at 04:52
  • Thanks! That was the problem. This also pops up some other questions but those are more theoric and i'll ask my professor tomorrow. Thanks again. – Martin Oct 24 '22 at 10:49
  • From *[Referencing the contents of a memory location. (x86 addressing modes)](https://stackoverflow.com/q/34058101)* - *The registers all have to be the same size as each other. And **the same size as the mode you're in** unless ...* In 64-bit mode, use 64-bit registers in your addressing mdoes. – Peter Cordes Oct 24 '22 at 11:04

0 Answers0