3

I am writing NASM assembly code, and have to do some indexed addressing. I have the index stored in $al, but x86 won't let you use $al as an index register, and I'm already using $bl, so I cant use $bx. So I need to put the byte I have in $al into a 32-but register such as $ecx, however, when I try, it throws an invalid combination of opcode and operand error. Is there any way to do this?

    sub     al, 97                  ; char - 97

    push    ecx                     ; b/c al cant be used as indexing register
    mov     ecx, al                 ; move byte in al into ecx

    mov     bl, [table + ecx]       ; value_at(first_table_addr + char) -> bx

    pop     ecx
phuclv
  • 37,963
  • 15
  • 156
  • 475
Ben Granger
  • 481
  • 6
  • 19

1 Answers1

11

Use the MOVZX instruction:

movzx ecx, al  ; move byte to doubleword, zero-extension

There's also MOVSX if you want the value in al to be treated as signed.

Zero-extention means the upper bits of the destination operand will be set to zero, while sign-extension means the upper bits of the destination operand will be set to the sign bit of the source operand. Some examples:

mov al,0x7F
movzx ebx,al   ; ebx = 0x0000007F
movsx ebx,al   ; ebx = 0x0000007F

mov al,0x80
movzx ebx,al   ; ebx = 0x00000080
movsx ebx,al   ; ebx = 0xFFFFFF80
Michael
  • 57,169
  • 9
  • 80
  • 125
  • so as I understand it, the 'zero-extension' just fills the remaining 24 odd bits in ecx with zeros?, so whatever was there doesn't muck up the small number I'm putting in? – Ben Granger Sep 29 '15 at 06:01