The MASM Forum Archive 2004 to 2012

General Forums => The Campus => Topic started by: ecube on October 09, 2007, 04:46:01 AM

Title: how you define a unsigned char in masm?
Post by: ecube on October 09, 2007, 04:46:01 AM
is it just a DWORD ?
Title: Re: how you define a unsigned char in masm?
Post by: Shell on October 09, 2007, 06:14:19 AM
It is a BYTE
byteVar DB ?

Whether signed or unsigned depends on how you use it eg. JG vs JA  :U
Title: Re: how you define a unsigned char in masm?
Post by: Mark Jones on October 09, 2007, 06:37:30 AM
One could always use a dword (4 bytes) to represent an unsigned char (1 byte), but three additional bytes will be wasted. With the huge amount of memory and speed of today's computers however, this is hardly a problem. It is usually easiest to just use dwords (and qwords) for everything nowadays. When speed or size becomes critical however, then look at smaller-size units such as word (2 bytes) and bytes.

In MASM, many other instructions and functions assume you are using dword-sized values or memory locations. Many of these will not work if you try to use words or bytes.

A "char" is really a byte, and "unsigned" just means that its value is interpreted as ranging from 0 to 255 instead of "signed" which is interpreted as +/-0 to 127. A number is considered "signed" when its most significant bit (MSB) is set.

.data?
myDB  db  ?    ; myDB is one byte of reserved memory
myDW  dw  ?    ; DW word, two bytes
myDD  dd  ?    ; DD double word, four bytes
myDQ  dq  ?    ; QW quad word, eight bytes

.code
mov byte ptr [myDB],123        ; put 123 into myDB (char)

mov byte ptr [myDW],55         ; put 55 into SECOND byte of myDW
mov byte ptr [myDW+1],33       ; put 33 into FIRST byte of myDW

mov dword ptr [myDD],11223344h ; put 0x44332211 into myDD

mov word ptr [myDQ+2],3412h    ; make myDQ equal 0x0000000012340000

Title: Re: how you define a unsigned char in masm?
Post by: sinsi on October 09, 2007, 07:26:49 AM
From the masm32 windows.inc (TheAuthority heh heh)
Quote
; 8 bit   BYTE
; ~~~~~
CHAR                        typedef BYTE

Aren't all char's unsigned anyway? (talking ansi here...not unicode)
Title: Re: how you define a unsigned char in masm?
Post by: zooba on October 09, 2007, 07:48:49 AM
No, they aren't all unsigned. C/C++ standards have char signed by default. The non-intrinsic definition of wchar_t is unsigned short, so those are unsigned.

Personally, I prefer to stick to BYTEs and DWORDs. I find dealing in WORDs (with the exception of UTF-16 strings) cumbersome and can bloat code. Also, you don't gain any performance benefits from working in WORDs instead of DWORDs, and as Mark says, the space issues don't really exist anymore.

Cheers,

Zooba :U
Title: Re: how you define a unsigned char in masm?
Post by: sinsi on October 09, 2007, 07:55:26 AM
Quote from: zooba on October 09, 2007, 07:48:49 AM
Personally, I prefer to stick to BYTEs and DWORDs.
Well said - I know no C/C++ (although I need to know some (think SDK/DDK)), so wading through .H files is a headf*ck

As someone's sigline says, "everything is DWORD"
Title: Re: how you define a unsigned char in masm?
Post by: ecube on October 09, 2007, 06:43:01 PM
thanks for the response guys, appreciate it :D