News:

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

Data Type Problems...

Started by dicky96, January 20, 2006, 07:33:59 PM

Previous topic - Next topic

dicky96

I didn't expect these sort of problems with assembler but it seems masm does not understand what I want to do and is imposing a "data type" restriction on me.  Most likely I have the syntax wrong, so could someone with a little experience help me sort a couple things out? Her is the sort of thing I want to do.

example1

.data
logXkey db '00X',0    ;I want to use these keys to access privateprofilestring in an ini file
logYkey db '00Y'0

.code
mov al,3031h           ;ascii representation of 01
mov [logXkey],al      ;change key to '01X'
.....
.....
inc al
mov [logXkey],al      ;change key to 02X

I would have thought masm would have happily moved a word (size of al) to memory address logXkey but I get an error - invalid instruction operands

If I try to move a byte (al) then that seems to be OK with masm, but I wanted to move a word


Another similar problem.....

example2.

.data
Buffer db 256 dup (0)  ; - my code puts the default string 'XX' here if no proper data is available


.code    ;now I want to check contents of first two bytes stored in Buffer
mov al,[Buffer]
.if al==5858h  ; ascii equiv of XX
   ............
.else
  .............
.endif

Again I get an error  - error A2070: invalid instruction operands

Similar problem to the first one.  I didn't think assembler was a "strongly typed" language like C.  In fact I didn't really think it was "typed" at all as long as the assembler can figure out the size of the operand, which I would say it could do above from the register size. 

What am I doing wrong?

TIA
dicky





tenkey

Quote from: dicky96 on January 20, 2006, 07:33:59 PM
What am I doing wrong?
You're assuming that MASM is weakly typed, which is true of most other assemblers. But not MASM. Maybe someone else can correct me, but I believe MASM is only interested in size matches. (Ignoring the need to specify structure type for accessing structure fields.)

If a label is associated with a BYTE size (with db), then you need to override that association with

mov word ptr[Buffer], ax
mov ax,word ptr[Buffer]
A programming language is low level when its programs require attention to the irrelevant.
Alan Perlis, Epigram #8

raymond

Or you do it byte by byte. For example,
mov logXkey[1],"1"
mov logYkey[1],"2"


Same for comparisons
.if Buffer == 58h && Buffer[1] == 58h
   ............
.else
  .............
.endif


Raymond
When you assume something, you risk being wrong half the time
http://www.ray.masmcode.com

Ian_B

Quote from: dicky96 on January 20, 2006, 07:33:59 PMI would have thought masm would have happily moved a word (size of al) ...

If I try to move a byte (al) then that seems to be OK with masm, but I wanted to move a word

That's the source of your problem, right there. AL is NOT a WORD-size register, it's a BYTE-size register. AX is the 2-byte WORD-sized addressable portion of EAX, not AL.

Quote
.if al==5858h  ; ascii equiv of XX
   ............
.else
  .............
.endif

Again I get an error  - error A2070: invalid instruction operands
Quote

Which is correct, because the compiler knows that AL can only contain a byte-sized data value, and you are testing for a WORD-sized value. Load the value into AX and test that, and you'll be fine.

IanB

dicky96

@tenkay
Thanks - I'll have a play around with that.

@Ian B
Sorry m8 that was a typo - I meant to say the  size of AX.

mov ax,[Buffer]
.if ax==5858h  ; ascii equiv of XX
   ............

That is the code that gives me the error.

I know I could move two bytes separately to ah and al.  My long string of ascii is actually a hexadecimal representation of binary data.  I need to convert that back to binary (if you see what I mean) so for example A7h will be a byte (10100111) rather than two bytes 41h 37h as it is now.  That's why I was thinking it better to treat my sequence of bytes as a sequence of words - I need two ascii bytes to form each single binary byte after some ANDing ORin and SHL stuff so  Iwas hoping to load them into a 16 bit register and manipulate them there. :wink

Actually on the topic is there some library routine I can use to do this, I know of things like atoi but they seem more designed to deal with numbers rather than long sequences of binary data.

Otherwise no problem - I'm working on my own code to do what is needed.

While I have all your attention can you tell me why this does not work...

.data
XX equ 5858h

.code
.if ax==XX


And also is it possible to get masm to accept hex values in the form 0xABCD rather than 0ABCDh?  This is just for personal preference.  When I tried this I got some error message....  I only found the 0ABCDh format was OK after a little experimentation.

dicky

Ratch

dicky96,

Quote
While I have all your attention can you tell me why this does not work...

.data
XX equ 5858h

.code
.if ax==XX

     Works fine, what's the problem?



= 00005858 XX equ 5858h
00000000 .code
00000000 START:
.if ax==XX
00000000  66| 3D 5858    *     cmp    ax, XX
00000004  75 01    *     jne    @C0001
00000006  90     NOP
.ENDIF
00000007    *@C0001:



Quote
And also is it possible to get masm to accept hex values in the form 0xABCD rather than 0ABCDh?  This is just for personal preference.  When I tried this I got some error message....  I only found the 0ABCDh format was OK after a little experimentation.

     No, see http://webster.cs.ucr.edu/Page_TechDocs/MASMDoc/ProgrammersGuide/Chap_01.htm about coding Integer Constants and Constant Expressions.  Ratch