Class of October 12, 1996


DISCLAIMER: These notes, hopefully not riddled with errors, may not be
complete but are offered for whatever value they may have to my
classmates.  These notes issue from the pen of Terry Winter Owens, wife of
the esteemed professor.

Review material prompted by questions from the class:

	Question about the word pointer. Pointer, when used as an
identifier, has no special meaning, and is merely a label, chosen by the
programmer. 

	To make a pointer to a location:
		.model small
		.stack 100h
		.data
			array dw 18h, 19h, 20h
			pointer dw array ;value (offset) of symbol array
					    is zero
		

	Explanation of what 'pointer dw array' does: It will put the value
zero at the word labeled pointer since array is at offset 0000.

		.code
			.
			.
			mov ax,pointer 	;puts 0000 into ax

We have to learn syntax for techniques (such as the above) that we will
need in the future but that do not necessarily have an immediate
application.

Syntax means the rules for allowable formations, in this case, in
assembler language.

More on labels, identifiers, variables:

	Label and variable are types of identifier.  Altho they have
slightly different meanings, they are often used as synonyms.
	Identifiers have default length of from 1 to 31 characters, can
contain alpha, special characters, numeric.  Must start with alpha , altho
first character can be $ _ @
	Examples of allowable identifiers:
		_job
		_011
		@@12

	However, +12 is illegal, + cannot be used to start identifier.

	Labels are defined, recognized by their appearance.
		L: mov ax,3   ;L equals value of location counter at this 
				line.

	$ (dollar sign) is specific symbol for location counter.

		x dw $-3 ; puts the present value of location counter
			   with 3 subtracted from it into the word at
			   location 'x'


Question about addresses:

	loc is a relative address.
	offset is distance from some starting point, some address in
memory.  The offset part of the address is made of up two bytes (two bytes
= one word). The segment part of the address is also made up of two bytes.

		segment offset
	
As the assembler puts code together, it keeps track of addresses with a
location counter. Address, location, offset refer to places in memory,
each with a special qualification.

	
Question from page 48 in re strings:

	.data 
		x dw ?,?,?  	;starts at offset zero
		astr db "Good morning programmer" ;starts at offset 6

	lnst dw $-astr  ; $-astr equals 17h
		 	; $ has value 1D, contains 0017h 

	mov ax, lnst ; puts 17h in ax
	mov ax, offset lnst ; will put 001D into ax
	mov bx, lnst ; loads 17h into bx

Question about Page 66 line 10

	$ in quote marks '$' is not a reference to the location counter
but an ASCII value. In this case, '$' acts as a terminator or stopper. If
you need to use the dollar sign as part of your string, you will need to
use a different dos function, one that does not require '$' for
termination. 

Question about define byte and define word:
	
	p db 9	;takes 1 byte - stores 09
	pq dw 9 ; takes 2 bytes - stores 0009

Largest positive numeric value in a byte is 127. 

Question about int 21h
	int 21h is a software event, calls dos.  Dos then offers a
multitude of services. When a programmer creates an interrupt (as opposed
to a hardware event), it is more appropriately called a DOS function call. 

	21h looks at ah and dx to do its job.

	ah 	; the value in ah tells dos what to do. If contents 
of ah is 09, then when the int21 takes place, it will output ASCII
characters to the screen until it encounters a '$' 
	dx	; shows where to get the information to print.
		; dx points to beginning of string

Question re Page 51

	db 4 dup ('qrs') ; repeat byte four times;
			 ; 'qrsqrsqrsqrs'

Question about indirect and direct addressing modes:

	.data
		x dw 3
		y dw 4

	.code
		.
		.
		mov ax,x  		;direct addressing
		mov si, offset x  	; direct
					; loads offset of x into si
					  which happens to be 0000
		mov ax,[si]		; indirect addressing
					; loads value stored at 000 which
					  is 0003

Mov operations are hard-wired.  Approx 30 different kinds of mov,
depending on operands. Machine op code is different for each kind of mov.
Asssembler looks at operands, selects appropriate op code to perform the
mov. 


Reserving space in memory:

	db 213 dup (?)			;reserves space for 213 bytes of
					 as yet unspecified values.
	zero db 214 dup (0)		; will put 0 into 213 adjacent
					  locations

Questions about inc and dec:

	inc ax		;adds 1 to value at ax
	inc loc		; adds one to value stored at loc
	
	Example: 
	loc dw 3,4	; reserves two words with 0003 and 0004
	loc db 5	; sets aside a byte with 05
	inc loc 	; points to a 3 and increments it by 1
	inc loc+2 	; points to the 4 and increments it by 1
			
dec and inc also have various machine op codes, depending on operands
used.

Question about xchg

		mov ax,3 
		mov bx,0FFFFFFh
		xchg ax,bx	;after execution, ax will have 0FFFFFh and
				 bx will have a 3

xchg CANNOT exchange memory to memory


		x dw 3
		y dw 18

There is no instruction to do the following: xchg x,y

In order to do this, (to exchange values at two memory locs), you have to
load registers, as follows:

		mov ax, x	;puts a copy of contents of x, which is
				3,into ax
		xchg ax,y	; exchanges contents of ax with memory
				location y. y now is 3 and ax is 18
		xchg ax,x	;exchanges ax and x so ax now has 3 and x
				has 18

Alternatively could have been done as follows:
 
		mov ax,x
		mov bx,y
		mov y,ax
		mov x,bx
in this example, x and y are still at the same place, only their contents
have changed.

xchg can exchange register to register and register to memory but NOT
memory to memory.  

Question about using td (turbo debugger)

	To use td, your program has to be assembled and linked 
	tasm /zi /l progname 
		/zi option puts debugging information into the code. 
		/l option creates an .lst file	
	tlink /v	option v makes an .obj into an .exe

	
Question about operands:
	Three kinds: symbols, literals and registers
		mov reg, reg ;works for most but not all register.
Registers must be compatible. For example: mov ip,3 is not a wired
possibility.

		mov reg,symbol (loc)

		mov reg, immediate such as mov ax,3			

In all three cases, the direction is from right to left
		mov dest, source ; in all cases, source is NOT changed.

Question about pushf and popf

	flag register is 16 bit and can be saved to stack by pushf. popf
restores flags. 

	push ax		;pushes register onto stack
	push loc	;pushes contents of loc onto stack

Subroutine is code that are or can be repeated over and over
again. In .code segment, you can call a subroutine with
	call subx 	; the assembler saves the register on the stack.
			When subroutine is finished, it restored the register. 
	
The stack (an area of memory) is used to save and restore registers. 
SS register points to segment address of stack
SP is offset of stack; that is SS:SP holds complete memory loc of stack.
stack is used by hardware interrupts.
To allocate stack size:
	.stack 100h
	.stack 1000h
Stack is created at run time.  Each program has its OWN stack.

push and pop are used to hold and retrieve intermediate results.
	

NEW MATERIAL:

ADD and SUB   destination, source  (Source is not changed by these
operations)
			

can be used in the following ways:
		add register, register	
		add loc, register
		add loc, immediate
		add register, immediate

Whether values are signed or unsigned is up to the programmer.
Instructions set the flags.  You have to examine sign flag,
carry flag, overflow flag to make sure what took place is what you
intended. 

ADDRESSING MODES  (very complex, many techniques, requires much study.)

	.data
		mem dw 1,4,7
	.code
		mov ax,3	;direct and immediate
		mov ax[si] ;indirect.  Uses si register to point to a location in
memory.  si holds the offset part of the address of the mem loc where the
source operand is to be found.  it is equivalent to: 
		mov ax,[ds:si] ; a two byte operand in memory which is 
				copied into ax

MODIFIERS:
	.data
		mem dw 1,4,7
	.code
		mov ax,mem+2 ;+2 is a modifier. Adds 2 to the offset
				value of mem and copies a value from
				that place in memory

ADDING NUMBERS:
		
	.data	
		x dw 1,3,5,7 	; define 4 words
		sum dw 0	;variable in which the sum will be held

	.code
		mov ax,@data	;puts seg address of data in ax
		mov ds,ax	;loads ds with seg address.  Cannot be
				done in one step, so have to load ax 
				and then from there, load ds. 

		mov ax,x
		add ax,x+2
		add ax,x+4
		add ax,X+6
		mov sum,ax

another approach using indirect addressing:
		(see explanation of xor in next paragraph)

		xor si,si
		mov ax, mem[si]
		add si,2
		add ax,mem[si]
		add si,2
		add ax,mem[si]
		add si,2
		add ax,mem[si]
		mov sum,ax


XOR
	exclusive or defined as either a OR b, but NOT both
		xor si,si	;sets si to zero

		when you xor a register with itself. the register is set to zero. 
mov si,0 is slow, inefficient, takes more than on machine cycle. 
Therefore, it is preferable to use xor.  However, you can NOT xor some
registers such as IP. 

Truth table for xor

	a  |  b     a xor b

	0     0		0
	
	0     1		1

	1     0		1

	1     1		0


Example of another method of adding, using indirect addressing: (refer to
example on ADDING NUMBER above.)

	xor si, si	;set si to zero
	mov ax,x[si]	
	inc si		; have to increment segment address twice
	inc si		; because we are working with words rather than
			   bytes
	add ax,x[si]	;add x[si] to ax
	inc si		; Jimmy Durante developed this technique
	inc si		; Ink a dink a dink 
	add ax,x[si]

This is slow and tedious method.
ADDING NUMBERS USING A LOOP

	mov ax,x
	mov cx,3 	; cx will keep track of number of times to perform
			the loop.  It decrements the initialized value
			each time the loop executes and when it reaches zero, 
			it terminates. The equivalent in C of if (--cx) go
			to label.

	adder: add ax,x[si]
		inc si
		inc si
		loop adder	;first does --cx.  If cx not equal
				zero, it goes to label adder: Performs
				add,inc, inc and again evaluates cx, etc.	
	mov sum,ax		

Another way:
	mov bx, offset x
	mov ax, [bx]
	add ax,[bx+2]
	add ax,[bx+4]
	add ax,[bx+6]
	mov sum, ax	

For a two dimensional array:

	mov ax,mem[bx+si]	;bx points to column, si points to row

This covers  most of chapter 3.

PROBLEM 9: (with direct addressing)

	;Copy array to another variable

	.data
		ary dw "ab','cd','ef' ; creates an array at offset zero
					three words having these values
					6162 6364 6566.
					(In memory these bytes are
					 switched) 		
		aryptr	dw ary	      ; has offset value of 6 -- that is
					the array itself takes up the
					first three words (starts at zero)
		
		arycpy	dw 0,0,0	; initialized to zero.  Has 
					offset 7 (starting one word after
					aryptr

		aryptr2 dw arycopy	; destination pointer
					; has offset value 13

	.code				
		mov ax.@data
		mov ds,ax
		mov ax, ary
		mov arycpy,ax
		mov ax,ary+2
		mov arycpy+2,ax
		mov ax, ary+4
		mov arycpy+4,ax


Problem 9 using indirect addressing

		mov ax,@data
		mov ds,ax

		mov si,aryptr 
		mov di,aryptr2
		mov ax[si]	;loads CONTENT of si into ax
				; content is 6162
		mov [di],ax	; di points to arycpy
				; di now has 6162
				; we have moved one word
				; to repeat for next work, inc si and di
		add si,2	; inc si inc si is actually faster than
		add di,2	; adding two 
		mov ax,[si]
		mov[di],ax


PROBLEM 9 USING A LOOP

		mov cx,3	;initialize counter to execute 3 times
			mov si,aryptr
			mov di,aryptr2
		start:	mov ax,[si]
			mov [di],ax
			add si,2
			add di,2
			loop start