Reputation: 101
I am using the YASM assembler.
If I have a variable declared as such
segment .bss
number resb 100
and I perform a logical right shift like so
shr byte [number], 8
and if for example 123 is stored in there so that the memory looks like such 0x333231 then I expect the result to be 0x3332 but the result is instead 0x333200. This problem does not occur if I have the data stored in a register, could anyone explain to me why this occurs and how to fix it (I would like to use memory and not a register).
Upvotes: 0
Views: 124
Reputation: 39306
for example 123 is stored in there so that the memory looks like such 0x333231
Seeing the value 0x333231, I dare assume that the memory at number holds digits in ASCII representation.
31 32 33 00 00 00 ... 00
A shift right by 8 bits would therefore shift out the lowest digit. You don't need the shr
instruction to do that. Just copy the memory:
mov edi, number
lea esi, [edi+1]
mov ecx, 99
cld
rep movsb
mov [edi], cl ; CL=0
The above code does to the whole 100-byte buffer, what below code can do for the first 4 bytes only.
mov eax, [number]
shr eax, 8
mov [number], eax
or
shr dword [number], 8
If we consider the 100-byte number as a bitstring, we can shift its contents down by counts other than 8:
mov ebx, number
mov eax, [ebx]
More:
mov edx, [ebx+4]
shrd eax, edx, 4 ; Shift count [0-31]
mov [ebx], eax
add ebx, 4
mov eax, edx
cmp ebx, number+96
jb More
shr eax, 4 ; Shift count [0-31]
mov [ebx], eax
Upvotes: 2