prev, up, next.

An example assembly language program

(This is not a complete program)


data	segment
count	db			; counter variable
data	ends

code	segment
	assume cs:code,ds:nothing,es:data,ss:stack
main:
	mov	ax,seg data	; set es register
	mov	es,ax		; to point to data segment
	mov	count,10	; initialize counter to 10
loop_top:
	dec	count		; decrement counter
	jnz	loop_top	; if counter is not zero, repeat loop
	mov	ax,4c00h	; set up for exit
	int	21h		; call exit interrupt
code	ends

This corresponds roughly to the following C program:


int main() {
  int a=10;        /* counter variable */

  while( a != 0 )
    a--;
  exit(0);
}

You don't have to worry about the specifics of the assembly-language program above, but I'll point out some of the features described above. Note that each instruction or pseudo-instruction line has a comment along with it. You really ought to comment your programs to this level, as well as using entire comment lines to mark off and describe sections of the code. The pseudo-operations include the segment directives which declare segments, the assume directive, which tells the assembler which segments correspond to which segment registers, the db directive which reserves a space in the data segment for a byte-sized variable, and the ends directive which shows the end of the segment. The instructions include mov, which moves data from one place to another, dec, which decrements a register or memory location, jnz, a conditional jump instruction, and int, which calls an operating system function via an interrupt. There are two labels in the program: main, which just refers to the top of the main procedure, and loop_top, which is given as the target of the conditional jump and marks, amazingly enough, the top of the loop.

prev, up, next.