开发者

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?


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


Try this code:

; xor esi,esi - not needed because the next line will overwrite esi anyway.
mov esi,OFFSET array     ; Moves array in to ESI for offset
inc esi                  ; incrementing to point to fib3 value
mov bl,[esi+2]             ; fib5 temporarily going to bl register
mov bh,[esi+3]             ; fib6 temporarily going to bh register
shl ebx, 16             ; fib5,fib6 moving to high half of EBX register
mov bl,[esi]             ; fib3 going to bl register
mov bh,[esi+1]             ; fib4 going to bh register
add esi,4                  ; now esi will point to fib7 value
; EBX register now has all for values (from high to low end of EBX): fib6, fib5, fib4, fib3
; END Of code.

Of course, since each value takes one byte = 8 bits, and size of EBX is four bytes (=32 bits), you cannot put more than 4 byte-sized values into EBX.

I have no idea what you really want to do, but here is a good exampe, how to pack four byte-sized values into EBX register.

You should note, that BL = 8 low bits of EBX, and BX = 16 low bits of EBX.

In other words:

BL = bits[7..0] of EBX.

BH = bits[15..8] of EBX.

BX = bits[15..0] of EBX.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜