News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests

Main Menu

2 forms of PTR instructions

Started by StillLearningMasm, October 09, 2023, 12:50:13 PM

Previous topic - Next topic

StillLearningMasm

I did not know that masm supported 2 different forms of the PTR operator.
Notice PTR and size is inside [] in one form but outside the other form.
form1:   mov [size PTR register],number
form2:   mov size PTR [register],number

The same can be done when all numbers are used.

I also thought that the results were the same. This does not seem to be always true.
Here are some examples:

.286
.model small
.data

PtrWORD typedef ptr WORD
PtrDWORD typedef ptr DWORD

.code
;Why does form1 sometimes cause errors and form2 does not?
mov ds:[word ptr 20],0 ;no error in MASM
mov word ptr ds:[20],0 ;no error in MASM
mov ds:[PtrWORD PTR 20],0 ;no error in MASM
mov PtrWORD PTR ds:[20],0 ;no error in MASM

mov [word PTR bx],0 ;causes error in MASM, why?
mov WORD PTR [bx],0 ;no error in MASM, why?
mov [PtrWORD PTR bx],0 ;no error in MASM, why?
mov PtrWORD PTR [bx],0 ;no error in MASM, why?

.386
;check WORD
;Why does form1 sometimes cause errors and form2 does not?
mov ds:[word ptr 20],0 ;no error in MASM
mov word ptr ds:[20],0 ;no error in MASM
mov ds:[PtrWORD PTR 20],0 ;no error in MASM
mov PtrWORD PTR ds:[20],0 ;no error in MASM

mov [word PTR ebx],0   ;causes error in MASM,,should not, why?
mov word PTR [ebx],0   ;no error in MASM, why?
mov [PtrWORD PTR ebx],0 ;causes error in MASM,should not, why?
mov PtrWORD PTR [ebx],0 ;no error in MASM, why?

;now check DWORD
mov ds:[dword ptr 20],0 ;no error in MASM
mov dword ptr ds:[20],0 ;no error in MASM
 ;Why are these 2 assembled instructions different then the 2 above?
 ;NOTE: if the type definition for PtrDWORD is changed to this:
 ;        PtrDWORD typedef DWORD
 ;      then the same code is created above. Notice PTR is missing.
mov ds:[PtrDWORD PTR 20],0 ;Both of these and the 2 above should all be
mov PtrDWORD PTR ds:[20],0 ;the same assembled instructions. But are not.
                           ;these 2 instructions have code created for WORD
                           ;not DWORD, Why?

mov [dword PTR ebx],0   ;causes error in MASM,should not, why?
mov dword PTR [ebx],0   ;no error in MASM
 ;Why are these 2 assembled instructions different then the 2 above?
mov [PtrDWORD PTR ebx],0 ;causes error in MASM, should not
mov PtrDWORD PTR [ebx],0 ;no error in MASM. Code is created for WORD
                         ;not DWORD, why?
end

Why does the existance of PTR in the typedef definitions have a
affect on the instructions created when the instuctions created
have a PTR in them?

Can anybody explain any of this?

Thank you for any help,

StillLearningMasm

NoCforMe

Just what the hell are you actually trying to do? Seems like all you're presenting us are weird outlier MASM errors for stuff that nobody actually uses ...
Assembly language programming should be fun. That's why I do it.

NoCforMe

Look, if your main complaint here is that things like this don't work:

mov [dword PTR ebx],0   ;causes error in MASM,should not, why?

then my advice is the punch line of the old joke:

"Doc, it hurts when I do this!"
"Then don't do that!"

Just use the regular old

mov dword PTR [ebx],0

and don't worry about why MASM gets all confused sometimes.
Assembly language programming should be fun. That's why I do it.

zedd

@ StillLearningMasm...
Seems that you are either using very old reference material, non Masm reference material, or are just plain experimenting.

I am curious to see any source code from you of a working program.

Since you are using ".model small", also leads me to believe that you are using very old stuff here.
What version of Masm (ml.exe) are you using?  And what version of the Masm32 SDK do you have on your system?
If my assumptions are correct, I will move this thread to the 16 bit board.

BugCatcher

mov [word PTR ebx],0   ;causes error in MASM,,should not, why?
[word ptr ebx] The brackets turns the whole thing into a pointer statement. But whats the type (or length of bytes) to write to?

mov word PTR [ebx],0   ;no error in MASM, why?
ebx is the pointer to memory. The assembler now knows to write length of a word to that memory location.

NoCforMe

Quote from: zedd151 on October 09, 2023, 09:31:02 PMI am curious to see any source code from you of a working program.
Me too. As I think are most of us here.

Don't worry, we don't bite.
Assembly language programming should be fun. That's why I do it.

satpro

The Campus
A protected forum where programmers learning assembler can ask questions in a sensible and safe atmosphere without being harassed or insulted. 

You know, I think that is a wonderful idea for a sub-forum!  When can we get one up and running?

zedd

#7
@satpro, who exactly has been harassed or insulted here? Btw, you just reminded me that I was going to move this thread out of the Campus, and to the 16 bit board.

.model small indicates to me that the OP is working with 16 bit MS-DOS code

zedd.
In reference to my post here: https://masm32.com/board/index.php?msg=124380

It does seem that StillLearningMasm is using outdated reference material, and has been doing so for quite some time:
https://masm32.com/board/index.php?msg=86514

jj2007

Quote from: zedd151 on October 09, 2023, 09:31:02 PMI am curious to see any source code from you of a working program.

satpro has a point here: this is/was indeed The Campus. OTOH I can understand that you are a bit fed up, Z. Me too.

StillLearningMasm please install the latest Masm32 SDK, and don't waste time on asking too exotic questions. Yes, MASM has quirks and bugs and strange "features", but if you really want to explore all of them, you are free to do so. Read the manual, test what you don't understand (trial & error are the programmer's best friends), and from time to time report to us what you found :cool:

Rockphorr

always use this formula: mov <type> ptr <address>,<source>

mov word ptr DS:[2],0

mov word ptr DS:[bx],0

mov byte ptr DS:[bx],0

mov word ptr DS:[bx],AX ;; corrected after NoCforMe notes.
mov byte ptr DS:[bx],AL

etc.....



Rockphorr

#10
PtrDWORD typedef DWORD
...
mov PtrDWORD PTR [ebx],0 ;no error in MASM. Code is created for WORD
                         ;not DWORD, why?

There is definition before - PtrDWORD typedef DWORD

so mov PtrDWORD PTR [ebx],0 translates as mov DWORD PTR [ebx],0

NoCforMe

Exactly. Like I said. If it ain't broke don't fix it.
Assembly language programming should be fun. That's why I do it.

NoCforMe

Quote from: Rockphorr on October 11, 2023, 06:52:50 AMalways use this formula: mov <type> ptr <address>,<source>

mov word ptr DS:[2],0

mov word ptr DS:[bx],0

mov byte ptr DS:[bx],0

Yes, except that in your last 2 examples
Quotemov byte ptr DS:[bx],AX
mov byte ptr DS:[bx],AL
The first one is invalid, as the "byte ptr" conflicts with the size of the source register which is a WORD **;
The "byte ptr" in the last one is completely unnecessary, as the source register gives the size, so "byte ptr" is implicit.

** I just confirmed this; MASM said "Illegal instruction operands".
Assembly language programming should be fun. That's why I do it.

Rockphorr

Quote from: NoCforMe on October 11, 2023, 07:02:54 AM
Quote from: Rockphorr on October 11, 2023, 06:52:50 AMalways use this formula: mov <type> ptr <address>,<source>

mov word ptr DS:[2],0

mov word ptr DS:[bx],0

mov byte ptr DS:[bx],0

Yes, except that in your last 2 examples
Quotemov byte ptr DS:[bx],AX
mov byte ptr DS:[bx],AL
The first one is invalid, as the "byte ptr" conflicts with the size of the source register which is a WORD **;
The "byte ptr" in the last one is completely unnecessary, as the source register gives the size, so "byte ptr" is implicit.

** I just confirmed this; MASM said "Illegal instruction operands".


yes, i fogot to correct when copy+paste

>The "byte ptr" in the last one is completely unnecessary, as the source register gives the size, so "byte ptr" is implicit.

I do so. Sometimes byte ptr DS:[bx] is modifed to like byte ptr DS:(struc_name ptr [bx]).coords and I use word for coords field, but writes to column only :)

zedd

Quote from: Rockphorr on October 11, 2023, 06:57:14 AMPtrDWORD typedef DWORD
...
mov PtrDWORD PTR [ebx],0 ;no error in MASM. Code is created for WORD
                         ;not DWORD, why?

There is difinition before - PtrDWORD typedef DWORD

so mov PtrDWORD PTR [ebx],0 translates as mov DWORD PTR [ebx],0
Yes, exactly. Why the need for using TYPEDEF's, StillLearningMasm?... seems to add confusion to what you want to accomplish.