0

I have reserved 2 bytes for a variable in NASM assembly,then what should be the type specifier when I move this variable to a ax/bx/cx/dx register.

var resb 2
mov rax,type_specifier[var]

Should the type specifier be "word" or simply "byte" ?

Also, how shall I move a 3 byte variable into a 4 byte register(ebx) ?

asn
  • 2,408
  • 5
  • 23
  • 37
  • To load 2 bytes, the size of that operand has to be `word. Unless you use `movzx` or `movsx`, the type specifier has to match the register width of the other operand. Re: part 2: https://stackoverflow.com/questions/47832367/how-to-move-3-bytes-24bits-from-memory-to-a-register – Peter Cordes Mar 31 '18 at 14:00
  • So you want `movzx eax, word [var]` to zero-extend the 2-byte object/integer into RAX. – Peter Cordes Mar 31 '18 at 14:01
  • Can u answer the 2nd part of the question ? – asn Mar 31 '18 at 14:25
  • 3 bytes is not "native" width, so if you know there's no risk to read 1 byte ahead/after the 3 byte area (triggering seg fault due to previous/next memory page being not readable), you can load 4 bytes like `mov eax,[var3b]` and then mask only 3 of them `and eax,0xFFFFFF`, clearing the 4th one. Or if you plan for example to do addition of two 3B vars, then you can just ignore the junk in the 4th byte. If you need to be precise (no junk-read-risk possible or writing back to memory only 3B), you will have to use two instruction reads/writes, like `mov [var3b],ax` `shr eax,16` `mov [var3b+2],al` – Ped7g Mar 31 '18 at 14:55
  • 1
    Generally try hard to stick with native sizes, and aligned memory allocations, that's for example reason why many graphic modes use 32 bits per pixel even when only 24 bits are effectively used to describe RGB 8bit-channel colour, with 8 bits waste per pixel, gaining the advantage on the alignment, and address calculation (making it worth to waste 1/4 of video memory). – Ped7g Mar 31 '18 at 14:58
  • @Jacob: https://stackoverflow.com/questions/47832367/how-to-move-3-bytes-24bits-from-memory-to-a-register is already my full and complete answer to part 2. Did you even read that link? Closed this question as a duplicate of the non-trivial part. The type_specifier part was already answered in comments. – Peter Cordes Mar 31 '18 at 16:32

0 Answers0