Search code examples
x86assemblynasmmasm

How to store consecutive bytes in the EBX register


I am trying to store an array of the the fibonacci sequence in consecutive bytes of the EBX register from lowest byte to highest byte. My code works as expected up until this point:

fib0=0
fib1=1
fib2= fib0 + fib1
fib3= fib1 + fib2
fib4= fib2 + fib3
fib5= fib3 + fib4
fib6= fib4 + fib5

.data
array BYTE fib2, fib3, fib4, fib5, fib6  ;Adding elements to the array

.code
main PROC

xor ebx,ebx
xor esi,esi
mov esi,OFFSET array     ; Moves array in to ESI for offset
inc esi                  ; incrementing to fib3 value
mov bl,[esi]             ; fib3 going to bl registry
inc esi                  ; inc. to fib4 value
mov bh,[esi]             ; fib4 going to bh registry

When I do a DumpRegs of EBX=00000302. When I then try to move fib5 to the bx registry it seems to over write the other two bl and bh registry values. So when I write the other two fib values to the ebx registry with the following code:

inc esi                  ; inc. to fib5 value
mov bx,[esi]             ; fib5 going to bx registry
inc esi                  ; inc. to fib6 value
mov ebx,[esi]            ; fib6 going to ebx registry

My final value for EBX=00000008 which means that final mov statement is completely overwriting the whole register. I would like for it to look like this EBX=08050302 in consecutive bytes. Is this possible?


Solution

  • When you write a value into a register, it starts at the lowest bits of the register. There is no way to access the high bits without accessing the lower ones, with the exception of the second byte with ah, bh, ch, and dh. You could do what you want by rotating the register right 8 bits after each storage, which will move the low byte to the high byte and the other bytes down 1.

        xor ebx,ebx
        mov cl,4                 ; 4 byte counter
        mov esi,OFFSET array     ; Moves array in to ESI for offset
    0:
        inc esi                  ; incrementing to next fib value
        mov bl,[esi]             ; fib going to bl registry
        ror ebx,8                ; Rotate right 8 bits
        dec cl
        jnz 0b