The MASM Forum Archive 2004 to 2012

General Forums => The Campus => Topic started by: ASMCoderD on June 09, 2010, 06:58:16 PM

Title: How do I output to a certain memory location?
Post by: ASMCoderD on June 09, 2010, 06:58:16 PM
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!
Title: Re: How do I output to a certain memory location?
Post by: qWord on June 09, 2010, 07:23:07 PM
maybe the org-directive is what you are asking for?
.code
org 100h    ; set location counter to 100h
...
Title: Re: How do I output to a certain memory location?
Post by: clive on June 09, 2010, 07:28:01 PM
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)
Title: Re: How do I output to a certain memory location?
Post by: ASMCoderD on June 09, 2010, 07:47:36 PM
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.

Title: Re: How do I output to a certain memory location?
Post by: redskull on June 09, 2010, 07:51:21 PM
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
Title: Re: How do I output to a certain memory location?
Post by: ASMCoderD on June 09, 2010, 07:58:00 PM
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
Title: Re: How do I output to a certain memory location?
Post by: redskull on June 09, 2010, 08:14:26 PM
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
Title: Re: How do I output to a certain memory location?
Post by: ASMCoderD on June 09, 2010, 08:18:56 PM
Thanks.

I'll have to keep trying at something.

David
Title: Re: How do I output to a certain memory location?
Post by: clive on June 09, 2010, 08:34:45 PM
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.
Title: Re: How do I output to a certain memory location?
Post by: ASMCoderD on June 09, 2010, 08:48:15 PM
Plenty of texts? Where?

David
Title: Re: How do I output to a certain memory location?
Post by: redskull on June 09, 2010, 08:54:05 PM
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
Title: Re: How do I output to a certain memory location?
Post by: ASMCoderD on June 09, 2010, 09:06:33 PM
Thanks.
Title: Re: How do I output to a certain memory location?
Post by: clive on June 09, 2010, 09:07:35 PM
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