News:

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

How do I output to a certain memory location?

Started by ASMCoderD, June 09, 2010, 06:58:16 PM

Previous topic - Next topic

ASMCoderD

How do I output code to a certain location, like say a DOS 100h file? I don't mean by linking, or something like that, or by using a preproccessor directive. I mean, is there a way to do it in the source code?

Thanks!

qWord

maybe the org-directive is what you are asking for?
.code
org 100h    ; set location counter to 100h
...
FPU in a trice: SmplMath
It's that simple!

clive

Beat me to it.

But, MASM is designed to be a relocatable assembler, with the linker resolving the address, if you don't use the -c option it will try to run the linker. There may be more appropriate assemblers/linkers combination. Unless you have an absolute assembler which basically generates binary/hex data directly, you usually need to use a linker to "fixate" the relocatable object (.obj)
It could be a random act of randomness. Those happen a lot as well.

ASMCoderD

The Org command is a directive of sorts, what I mean is, is there a way to do the equivalent with the source code? Without using a linker or the org command.

Thanks anyway. Maybe this isn't possible with MASM.


redskull

REP MOVS is how you would do it at runtime, but why would you ever want to?  All your memory addressess would be wrong.

-r
Strange women, lying in ponds, distributing swords, is no basis for a system of government

ASMCoderD

The reason is because I am making an assembler, as a project. But I need to know how to ORG my output programs. Already me assembler can translate opcodes like "mov ah, 0" and "mov al, 13" and "int 10" etc. But I would like to know how to ORG these to say 100h. Which ASM commands do this?

David

redskull

None, the address the code is located at has nothing to do with the code itself, it is function only of the assembler software (ie. directives) and/or linker.  You'll need to keep an internal program counter not only to track where the code is being assembled at, but also to calculate addressess when the time comes, and your own implementation of ORG to move it as needed.

-r
Strange women, lying in ponds, distributing swords, is no basis for a system of government

ASMCoderD

Thanks.

I'll have to keep trying at something.

David

clive

As noted the assembler is pretty much agnostic to the code location, and most code is address independent.

Most 16-bit near call/jumps are self relative. With far calls/jumps the addresses are absolute.

Your assembler will have to keep track of the instruction pointer for the code it is emitting, and will need to handle multiple segments if it gets more advanced. In order to make some address determinations (forward references, or in other segments) your assembler will need to take multiple passes over the source, or back patch. Multiple passes would be especially useful when trying to use the optimal short/long forms of various instructions.

It's your assembler, you can pretty much implement it's functionality anyway you choose. I would suggest however that you look for examples of how people have written other assemblers in the public domain, and open source arena. Perhaps look how some 8051, Z80 or other simple assemblers work, or for that matter line assemblers in debuggers/monitor type applications. Plenty of texts on the subject.
It could be a random act of randomness. Those happen a lot as well.

ASMCoderD


redskull

http://www.davidsalomon.name/assem.advertis/AssemAd.html

Not to mention there's an entire subfourm devoted to it here, plus any compiler book (of which there are thousands) will be useful in lexing, tokenizing, etc

-r
Strange women, lying in ponds, distributing swords, is no basis for a system of government


clive

Quote from: ASMCoderD
Plenty of texts? Where?

Well lets see, if we go back between now and 1970 (40 years), there are literally hundreds and thousands, of books, papers, college/university projects, specifically covering the implementation of assemblers, compilers, linkers and loaders. Do we honestly have to go find them for you? or could you just do a google search, or go to a local college library and find books on a pretty fundamental cornerstone of computer science?

http://www.amazon.com/Assemblers-Compilers-Program-Translation-Calingaert/dp/3540120343/ref=sr_1_1?ie=UTF8&s=books&qid=1276117587&sr=1-1

http://www.amazon.com/Program-Translation-Fundamentals-Methods-Issues/dp/0716781468/ref=ntt_at_ep_dpi_1

http://www.amazon.com/Principles-Compiler-Addison-Wesley-information-processing/dp/0201000229/ref=sr_1_4?ie=UTF8&s=books&qid=1276117988&sr=1-4
It could be a random act of randomness. Those happen a lot as well.