News:

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

how you define a unsigned char in masm?

Started by ecube, October 09, 2007, 04:46:01 AM

Previous topic - Next topic

Shell

It is a BYTE
byteVar DB ?

Whether signed or unsigned depends on how you use it eg. JG vs JA  :U

Mark Jones

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

"To deny our impulses... foolish; to revel in them, chaos." MCJ 2003.08

sinsi

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)
Light travels faster than sound, that's why some people seem bright until you hear them.

zooba

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

sinsi

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"
Light travels faster than sound, that's why some people seem bright until you hear them.

ecube

thanks for the response guys, appreciate it :D