Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
822 views
in Technique[技术] by (71.8m points)

assembly - Assembler use of segment register

Recently I was writing assembly and the program I wrote ran without any problems under DOSBox. Now I need to port the same program into a real computer using DOS but some problems arised.

First of all, under DOSBox I was compiling with ML, but on the real PC once I type in ML it says:

This program cannot be run in DOS mode.

Therefore I was looking for a solution amd found out that MASM can compile asm program without problems. Unfortunately, the program which I need to port reports severe errors (1 type only) while compiling.

error A2061: Improper use of segment register

The lines at which these problems arise are the following

...
CARLOC EQU $-2
...
MOV [WORD PTR DS:CARLOC],DX
...

Also the same problem arises with the following code

...
MOV ES,CX
MOV AL, [BYTE PTR ES:0017H]
...

So far I have tried to change this BYTE PTR into BYTE PTR [ES:0017H] which produced the same error

And into BYTE PTR ES:0017H which compiled the code successfully, the program ran but did not work correctly

Note: I do not know under which architecture am currently working. And probably won't be able to access the machine physically, but if there is some code that I can type in to see the information on screen I will be glad to do so.

Code is here, it is too long for here if i need to paste it here then ok, but until then https://pastecode.xyz/view/5f332efc

The PC says it runs MSDOS 6

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

(Update: Michael Petch says some MASM or MASM compatible assemblers allow size and segment overrides inside the brackets. But update2: not all, so this may actually have been the problem. It's at least more standard style so I'd recommend always doing that.)


In normal MASM syntax you want MOV [CARLOC], DX. Depending how you declared carloc, you may still need mov word ptr [CARLOC], dx, but DS: is already the default segment.

If you wanted to be explicit about it, MOV word ptr ds:[CARLOC], dx but I recommend leaving out redundant DS prefixes in the asm source because some assemblers include a redundant DS prefix in the machine code! The only time DS: isn't redundant is when used with ds:[bp] or ebp, or ds:[esp], which imply SS as the default segment.

With MASM, ds: prefixes are also requires with numeric absolute addresses. Otherwise that's treated as an immediate (regardless of brackets) which of course can't be a destination. With a definition like CARLOC equ $-2, you will need ds:[CARLOC]. Apparently MASM does not put useless ds prefixes in the machine code so you don't have to worry with that assembler.

If you need a CS/DS/ES/FS/GS/SS prefix, the standard syntax is to put it outside the brackets:

MOV AL, ES:[0017H]

The AL destination implies byte ptr operand-size, which also goes outside the brackets. Look at disassembler output for example.

And into BYTE PTR ES:0017H which compiled the code successfully,

Yes, that is valid syntax: the brackets are optional in some cases (including with absolute addresses or symbols). Many people recommend always using brackets around memory operands (as opposed to OFFSET symbol immediates) to make it clearer for human readers.

If you are going to use brackets, they go after the size ptr and seg: overrides.

the program ran but did not work correctly

Then either your code has other bugs beyond the syntax errors.

Or if you're trying to build this into a Windows executable: Of course it doesn't work to take code written for DOS (real mode, in control of the whole machine) and build it as a 32-bit or 64-bit Windows executable (protected mode or 64-bit mode, ring 3 under an OS). The system-call ABI and even API are totally different, too: those are different OSes and the DOS API isn't available to Windows executables.

Michael also suggested that a USE32 or other directive might make MASM try to save you from yourself and reject use of segmentation in code that's supposed to run with a flat memory model. But if es:[17H] works then that's probably not it.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
...