News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

any code to convert from a 32bit value to a byte ?

Started by Rainstorm, February 28, 2008, 07:35:43 PM

Previous topic - Next topic

Rainstorm

looking for some code that shows how to convert a 32 bit value to a byte value.

like if i had the value 25 in ebx how do i put it in a byte ?(like in al or dl for example)

thank you

hutch--

This is an easy one as long as your value is 255 or less.


mov ebx, value    ; write the 32 bit value to a 32 bit register.
; value is now in the BL register.
test al, bl
jz elsewhere

Download site for MASM32      New MASM Forum
https://masm32.com          https://masm32.com/board/index.php


hutch--

Just keep this in mind, on at least some hardware working on a BYTE part of a register can produce a pipeline stall which really slows your code down. Where possible do comparisons and the like with the full 32 bit register, even if you have to use MOVZX or MOVSX to get the other register up to 32 bit. There are a few exceptions which have previously cleared the 32 bit register.


xor eax, eax
cmp al, bl
.....
movzx eax, BYTE PTR [ecx+edx]
cmp al, BYTE PTR [esi+edx]


In both instances the register has been cleared by a 32 bit operation so the BYE operation does not stall after it.
Download site for MASM32      New MASM Forum
https://masm32.com          https://masm32.com/board/index.php

Rainstorm


hutch--

I should have made a bit more sense of the comment, if you perform a 32 bit operation on a register then pereform an 8 or 16 bit opertion on part of that register shortly after it can cause the stall. The problem is partial register reads after a full register write. The two examples I mentioned are the usual way around the problem. You can also use SUB REG, REG as well but XOR REG, REG is smaller.
Download site for MASM32      New MASM Forum
https://masm32.com          https://masm32.com/board/index.php