head	1.2;
access;
symbols;
locks
	yap:1.2; strict;
comment	@# @;


1.2
date	97.12.05.20.13.14;	author yap;	state Exp;
branches;
next	1.1;

1.1
date	97.12.04.16.21.47;	author yap;	state Exp;
branches;
next	;


desc
@@


1.2
log
@*** empty log message ***
@
text
@<html>
<title>
MEMORY MANAGEMENT: V22.0201, Machine Organization I, Yap
</title>
<body bgcolor="EEEEEE">

<h5><font color=AA0000> <center>
<a name=top>
V22.0201.002, Machine Organization I; Professor Yap <br>
Fall 1997
</a>
</center></font> </h5>

<img src="http://cs.nyu.edu/~yap/gifs/lines/rainbowline.gif">

<h3><font color=AA00AA> <center>
        NOTES ON MEMORY MANAGEMENT
</center></font> </h3>

<ol>
<li><b>
COMPACT MODEL
</b><br>
	In projects in which you need lots
of graphics images, it is likely that you need more
than one data segment.  E.g.,
storing a copy of the video buffer in mode 13h will take up
64000 bytes, leaving only about 1500 bytes for other stuff.
Since the standard SMALL MODEL
has one data segment, it is no longer adequate.
<p>
	First, suppose you need a simple extension
of the standard SMALL MODEL, in which you just
need just one other segment for data.  To do
this, you can use the standard COMPACT MODEL.
We illustrate how you
declare data for the extra data segment (called
the FARDATA SEGMENT), and how to use it.
	<pre>
	title Use of Fardata
	.model compact			; not small model
	.stack 100h
	.data
	; data declared here goes into the usual (NEAR) DATA SEGMENT
	len  dw 13
	s1 db 'Hello, World!'		; s1 has length 13 
	.fardata
	; data declared here goes into the extra (FAR) DATA SEGMENT
	s1 db 'Goodbye, All!'
	;  NOTE: the variable s1 in each data segments are distinguished
	.code
	;
	; ...
	; 
	; Here is how to use fardata.  Suppose you want to move
	;	the contents of s1 in @@fardata to s1 in @@data.
	mov ax,@@fardata		; or use the default segment name, _fardata
	mov es,ax
	assume es:@@fardata	; tasm needs to know
	lea si,s1		; because of ASSUME, this 
				;    refers to s1 in @@fardata 
	;
	mov ax,@@data
	mov ds,ax
	assume ds:@@data
	lea di,s1		; because of ASSUME, this
				;    refers to s1 in @@data 
	;
	mov cx,len
	rep movsb
	;
	; ...
	</pre>
<p>
	What if you need more than two data segments?
	Then use .fardata declarations with explicitly
	named data segments.
	<pre>
	.fardata dataseg1
	len dw 8
	s1 db 'very far'	; s1 has length 8
	.fardata dataseg2
	s1 db 'further!'
	s2 db 'unique'
	.code
	;
	; ...
	;
	mov ax,dataseg2
	mov es,ax
	assume es:dataseg2
	lea si,s1		; s1 in dataseg2
	;
	mov ax,dataseg1
	mov ds,ax
	assume ds:dataseg1
	lea di,s1		; s1 in dataseg1
	;
	mov cx,len
	rep movsb
	;
	; ...
	</pre>
	If the name of a variable (e.g., s2) is globally unique,
	you can also get its segment number as follows:
	<pre>
	mov ax, seg s2	; instead of mov ax,dataseg2
	mov ds, ax
	</pre>
<p>
	Also, if these operations of modifying ds and es occurs
	in a procedure, remember to save and restore
	the old values of ds and es.
<li><b>
Why ASSUME statement?
</b><br>
	The above use of ASSUME may initially
	appear redundant (after all, if an instruction needs to
	use es or ds, it will already have the correct
	values loaded in these registers).  But the
	example of non-unique names shows that the ASSEMBLER
	sometimes needs to know what the values of ds/es
	really represents.  Thus, when we say
	<pre>
	assume ds:dataseg1
	</pre>
	we are associating ds
	with the <b>data segment object</b> named ''dataseg1'', not
	associating ds with the <b>number</b> of the
	the data segment object named ''dataseg1''.
	In contrast, when we say
	<pre>
	mov ax,dataseg1
	mov ds,ax
	</pre>
	we are just loading a number into ds.

<li> The standard memory models are TINY (basically .com
	images), SMALL, COMPACT, MEDIUM, LARGE and HUGE.
	If you need segments that are greater than 64K,
	only the HUGE model will do. 

	<p>
	The default names of the segments in these standard
	models may be found in HELPPC (Assembler Programming
	Topics, under `segment names').  E.g. in the
	small model, the .code,
	.data, .const, .data? and .stack segments are
	named _TEXT, _DATA, CONST, _BSS and STACK, respectively.
<li>
In TASM, there are usually two ways
to issue segment related directives. E.g.
	<pre>
	.model ...
	</pre>
or
	<pre>
	model ...
	</pre>
The former form is to achieve compatibility with MASM.
The latter form is called the "ideal form".
Similarly, we could just say 
	<pre>
	FARDATA ...
	</pre>
instead of ``.fardata ...''.
<p>
	The ideal form for the segment directive is
	<pre>
	SEGMENT name [align] [combine] [use] ['class']
	</pre>

</ol>

<p>
<hr>
<a name=bot>
<a href=#top>
GO TO THE TOP
</a>
</a>
</body>
</html>

@


1.1
log
@Initial revision
@
text
@d137 13
@
