MEMORY MANAGEMENT: V22.0201, Machine Organization I, Yap

#####
V22.0201.002, Machine Organization I; Professor Yap
Fall 1997

###
NOTES ON MEMORY MANAGEMENT

1. COMPACT MODEL
   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.

   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.

       	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
       	;
       	; ...

   What if you need more than two data segments? Then use .fardata declarations with explicitly named data segments.

       	.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
       	;
       	; ...

   If the name of a variable (e.g., s2) is globally unique, you can also get its segment number as follows:

       	mov ax, seg s2	; instead of mov ax,dataseg2
       	mov ds, ax

   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.

2. Why ASSUME statement?
   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

       	assume ds:dataseg1

   we are associating ds with the data segment object named ''dataseg1'', not associating ds with the number of the the data segment object named ''dataseg1''. In contrast, when we say

       	mov ax,dataseg1
       	mov ds,ax

   we are just loading a number into ds.
3. 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.

   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.

4. In TASM, there are usually two ways to issue segment related directives. E.g.

       	.model ...

   or

       	model ...

   The former form is to achieve compatibility with MASM. The latter form is called the "ideal form". Similarly, we could just say

       	FARDATA ...

   instead of ``.fardata ...''.

   The ideal form for the segment directive is

       	SEGMENT name [align] [combine] [use] ['class']

GO TO THE TOP