Search code examples
arraysassemblyx86masmirvine32

Moving the memory address of a 32 bit register [esi] into an 8 bit low register in Assembly language x86


Is it possible to move the memory address of a 32 bit register [esi] into an 8 bit AL register? Can you explain how does that work? Here is my code that displays an array of numbers via for loop of 1 to 6:

TITLE printing          (printarray.asm)                     (special.asm)

;This  
;Last updated 92.15.2016 Written by dr scheiman
INCLUDE Irvine32.inc
.data
   arrayb byte 1,2,3,4,5,6
   l dword lengthof arrayb
   space byte "     ",0
   x dword 3
.code
main PROC
    mov edx,offset space
    mov eax,0  ; clear ecx of garbage
    mov ecx, l ; loop counter 
    mov esi,offset arrayb ; start of the array's memory
    myloop:
       mov al,[esi] ;how do you move the memory address of a 32 bit into an 8 bit register?
       call writedec
       call writestring
       inc esi
     loop myloop
     call crlf

 exit

main ENDP

end main

Solution

  • MASM infers the byte ptr [esi] operand-size based on the size of AL, and does an 8-bit load from the memory pointed to by the 32-bit pointer. The square brackets are a dereference for registers.

    You could zero-extend those 8 bits to fill EAX with movzx eax, byte ptr [esi]. (Then you wouldn't need to zero eax earlier).