St Louis Community College.

home

IS:112 SOFTWARE & HARDWARE CONCEPTS

ASSEMBLY LANGUAGE PRIMER

Process of creating and running an executable program

 

  1. Create a source program using an editor:
    • Using an editor, enter the source language statements.
    • Save the file.
  2. Assemble the source program, create an object program.
    • Use an assembler to translate the source program (statements written using the syntax of the language) into an object program (machine language instructions).
    • Correct errors detected by assembler. These corrections are made to the source program. The corrected program is assembled.
    • Saved as object file.
  3. Link the object program, create an executable program.
    • Resolves external references in the object program.
    • Fills in placeholders for offsets.
    • Writes an executable program file.
  4. Load and execute the program.
    • Loader creates a PSP (program header) in memory.
    • Memory allocated for program
    • Program placed in memory.
    • Loads CS, SS, ES with values that point to proper area of memory.

Sample Program that will multiply two times ten and display the result in hexadecimal.

sseg segment stack

stk dw 100d dup (?)

sseg ends

dseg segment

tot dw
hdr db 'Two times 10 is: $'
dseg ends

cseg segment

assume cs:cseg,ds:dseg,ss:sseg
main proc far
mov ax,dseg
mov ds,ax
mov ax,0
mov cx,10
lp: add ax,2
sub cx,1
jnz lp
mov tot,ax
mov dx,offset hdr
mov ah,9
int 21h
call prcrlf
call disptot
mov ah,4ch
int 21h
main endp

prcrlf proc

mov ah,2
mov dl,0dh
int 21h
mov dl,0ah
int 21h
ret
prcrlf endp

disptot proc

mov bx,tot
mov dh,4
mov cl,4
lp2: mov dl,bh
and dl,0f0h
rol dl,cl
call prnib
rol bx,cl
dec dh
jnz lp2
ret
disptot endp

prnib proc
cmp dl,9
jg a_f
add dl,30h
jmp wr_char
a_f: add dl,37h
wr_char: mov ah,2
int 21h
ret
prnib endp

cseg ends

end main

Microsoft (R) Macro Assembler Version 6.11

a:\mult.asm Page 1 - 1

0000 sseg segment stack

0000 0064 [ stk dw 100d dup (?)

0000

]

00C8 sseg ends

0000 dseg segment

0000 0000 tot dw ?

0002 54 77 6F 20 74 69 hdr db 'Two times 10 is: $'

6D 65 73 20 31 30

20 69 73 3A 20 24

0014 dseg ends

0000 cseg segment

assume cs:cseg,ds:dseg,ss:sseg

0000 main proc far

0000 B8 ---- R mov ax,dseg

0003 8E D8 mov ds,ax

0005 B8 0000 mov ax,0

0008 B9 000A mov cx,10

000B 83 C0 02 lp: add ax,2

000E 83 E9 01 sub cx,1

0011 75 F8 jnz lp

0013 A3 0000 R mov tot,ax

0016 BA 0002 R mov dx,offset hdr

0019 B4 09 mov ah,9

001B CD 21 int 21h

001D E8 0007 call prcrlf

0020 E8 000F call disptot

0023 B4 4C mov ah,4ch

0025 CD 21 int 21h

0027 main endp

0027 prcrlf proc

0027 B4 02 mov ah,2

0029 B2 0D mov dl,0dh

002B CD 21 int 21h

002D B2 0A mov dl,0ah

002F CD 21 int 21h

0031 C3 ret

0032 prcrlf endp

0032 disptot proc

0032 8B 1E 0000 R mov bx,tot

0036 B6 04 mov dh,4

0038 B1 04 mov cl,4

003A 8A D7 lp2: mov dl,bh

003C 80 E2 F0 and dl,0f0h

003F D2 C2 rol dl,cl

0041 E8 0007 call prnib

0044 D3 C3 rol bx,cl

0046 FE CE dec dh

0048 75 F0 jnz lp2

004A C3 ret

004B disptot endp

004B prnib proc

004B 80 FA 09 cmp dl,9

004E 7F 05 jg a_f

0050 80 C2 30 add dl,30h

0053 EB 03 jmp wr_char

0055 80 C2 37 a_f: add dl,37h

0058 B4 02 wr_char: mov ah,2

005A CD 21 int 21h

005C C3 ret

005D prnib endp

005D cseg ends

end main

Microsoft (R) Macro Assembler Version 6.11

a:\mult.asm Symbols 2 - 1

Segments and Groups:

N a m e Size Length Align Combine Class

cseg . . . . . . . . . . . . . . 16 Bit 005D Para Private

dseg . . . . . . . . . . . . . . 16 Bit 0014 Para Private

sseg . . . . . . . . . . . . . . 16 Bit 00C8 Para Stack

Procedures, parameters and locals:

N a m e Type Value Attr

disptot . . . . . . . . . . . . P Near 0032 cseg Length= 0019 Private

main . . . . . . . . . . . . . . P Far 0000 cseg Length= 0027 Private

prcrlf . . . . . . . . . . . . . P Near 0027 cseg Length= 000B Private

prnib . . . . . . . . . . . . . P Near 004B cseg Length= 0012 Private

Symbols:

N a m e Type Value Attr

a_f . . . . . . . . . . . . . . L Near 0055 cseg

hdr . . . . . . . . . . . . . . Byte 0002 dseg

lp2 . . . . . . . . . . . . . . L Near 003A cseg

lp . . . . . . . . . . . . . . . L Near 000B cseg

stk . . . . . . . . . . . . . . Word 0000 sseg

tot . . . . . . . . . . . . . . Word 0000 dseg

wr_char . . . . . . . . . . . . L Near 0058 cseg

0 Warnings

0 Errors

Selected DOS and BIOS Service Routines

BIOS Service 16/00: read keyboard character

  • AH: 00
  • call with INT 16H
  • on return, AH contains the scan code and AL contains the ASCII code (if any)

BIOS Service 1A/02: read real-time clock

  • AH: 02
  • call with INT 1AH
  • on return, CH, CL and DH contain the time in hours, minutes, and seconds

DOS Service 21/02: display single character to monitor

  • AH: 02 DL: ASCII code of the character to be displayed
  • call with INT 21H

DOS Service 21/05: print character

  • AH: 05 DL: character to be printed
  • call with INT 21H

DOS Service 21/09: display character string to monitor

  • AH: 09 DX: offset of string in current data segment; string must be terminated with a $ character
  • call with INT 21H

DOS Service 21/4C: end program execution and return to DOS

  • AH: 4C
  • call with INT 21H
  • space occupied by the program will now be eligible for being overwritten by a new program

DOS Service 27: terminate program but keep program resident in memory (TSR)

  • number of bytes to be kept in residence
  • call with INT 27H
  • space occupied by program will not subsequently be overwritten

Outline of assembly program:

Label1 SEGMENT ; stack segment

stack declarations

Label1 ENDS

Label2 SEGMENT ; data segment

data declarations

Label2 ENDS

Label3 SEGMENT ; code segment

ASSUME ; directive for CS, DS, SS to reference labels

label4 PROC FAR

set DS instructions ; instructions to load the DS register with data segment address

program instructions

label5: instructions

unconditional or conditional branch statement to label5 for loop

label4 ENDP

label6 PROC ; a procedure called from main program

ENDP

label3 ENDS

END label3

CODING FORMAT

Instructions follow a general format; [ ] indicates the item may (or not) be part of the instruction.

[name/label] operation [operand(s)] [comment]

  • In general, name refers to a data item, label refers to an instruction.
  • Name/label, operation, and operands are separated by at least one blank or tab character. They may begin in any column; however, starting these entries consistently in the same column makes a program more readable.

Name

  • A name or label can use alphabetic characters A-Z, a-z, digits 0-9, special characters ?, @, _, $
  • The first character must be alphabetic or special
  • Uppercase and lowercase are treated the same
  • Maximum length is 31 characters

Operation

  • For a data item, operation defines the size of the data item
  • For an instruction, operation indicates the action

Operand(s)

  • For a data item, operand defines the initial value
  • For an instruction, operand(s) indicate where to perform the action

DIRECTIVES

  • Directives, or psuedo-operations, are instructions to the assebler, not executable instructions
  • Act during the assembly of the program, do not generate machine-executable code

SEGMENT Directive

  • Defines a segment

name SEGMENT ;begin segment

:name ENDS ;end segment

PROC Directive

  • The code segment contains executable code
  • Contains one or more procedures, defined with the PROC directive
  • Procedure names must be present, must be unique, must following naming conventions
  • Operand of FAR indicates entry point for program execution to the DOS loader
  • Code segment may contain any number of procedures, these are NEAR

procname PROC

:procname ENDP

ASSUME Directive

  • The assembler needs to know the purpose of each segment.
  • ASSUME directive associates name of segment with segment register
  • Does not load addresses into registers

ASSUME SS:stacksegname,DS:datasegname,CS:codesegname

END Directive

  • END directive terminates entire program
  • May not be included if module is to be linked with another module

END procname of FAR procedure

PROGRAM ORGANIZATION (EXE)

Sequence for defining segments is usually unimportant - use stack, data, code order

INITIALIZING A PROGRAM

Two requirements for initializing are 1) let the assembler know the segments to associate with the segment registers (ASSUME); and 2) load the DS with the address of the data segment.

Two instructions initialize the DS register:

MOV AX,datasegname

MOV DS,AX

© 2017 St. Louis Community College
June 9, 2003