COS2621 Programs
COS2621 Programs
ws/Unisa
How to display output on the screen:
1. This program prints the character ‘a’:
org 0x100
bits 16
jmp main
message: db 'a' ; message is the address of char 'a'
main: mov bx, message ; move message (i.e. an address) to bx
mov dl,[ bx ] ; move contents of address pointed to
; by bx to dl. This is an example
; of register INDIRECT addressing
mov ah,02
int 21h
int 20h
Notes:
• mov ah,02 is used for displaying ONE character (in dl) only. If message
was ‘abc’, just ‘a’ would be displayed.
• To avoid indirect addressing, if you knew that the offset of message was
3, you could type mov dl,[103h] instead. It would be even simpler to type
mov dl,’a’ if you don’t need to give the character a label.
• Of AX, BX, CX, and DX, only BX can be used for indirect addressing.
org 0x100
bits 16
jmp main
message: db 'long string with no dollar/appostrophe characters!','$'
main: mov dx,message ; move the start address of message
mov ah,09
int 21h
int 20h
Notes:
• The string MUST end with ‘$’ - otherwise garbage follows in the output.
An optional 0ah, 0dh provide a line feed and carriage return.
• mov dx,message could be replaced with mov dx,103h because the offset of
message is 3.
Remember:
Character output - When ah contains 2, a character must be in dl.
String output - When ah contains 9, an address must be in dx.
org 0x100
bits 16
mov dl,'q'
inc dl ; increment a VALUE
mov ah,02
int 21h ; displays 'r'
int 20h
org 0x100
bits 16
1
http://wikistudent.ws/Unisa
mov dx,message
inc dx ; increment an ADDRESS
mov ah,09
int 21h ; displays 'werty'
int 20h
Notes:
• message may appear after the program!
mov dx,input
mov ah,0ah
int 21h ; a string has been read into dx;
mov dx,input
inc dx ; skip over the length of the buffer
inc dx ; skip over the no of bytes read
mov bx,dx ; bx points to start of input characters
mov al,[input+1]; the length of the string
mov ah,0 ; (this fills up ax)
add bx,ax ; bx points one char past last char entered
mov al,'$'
mov [bx],al ; put a $ sign after the last character
mov ah,09
int 21h ; the string in dx is displayed on the screen
mov dx,prompt2
mov ah,09
int 21h
mov ah,01
int 21h ; a character has been read into al
2
http://wikistudent.ws/Unisa
int 20h
Notes:
• When displaying the string that was read, you have to leave out the info
stored in the first two bytes, and add a $ sign to the end, with the help
of bx.
org 0x100
bits 16
jmp main
input: db 20
db 0
resb 21
mov dx,input
call get_string ; read in the string
mov dl,13
call display_char
mov dl,10
call display_char
mov dx,input
inc dx
inc dx
mov bx,dx
mov al,[input+1]
mov ah,0
add bx,ax
mov al,'$'
mov [bx],al
call display_string ; display the string
mov dx,prompt2
call display_string ; prompt for a character
mov dl,al
3
http://wikistudent.ws/Unisa
call display_char ; display the character
int 20h
Notes:
• When you want to display a character, you can use either single quotes
around the character, as in mov al,’2’, or a number which is the ASCII
equivalent, as in mov al,50 (which is also the character ‘2’). This won’t
work if you want to display a string that is in dx.
buffer: db ' ', '$' ; 5 spaces for max number of 65535 (FFFF)
int 20h
buffer: db ' ', '$' ; 3 spaces for max number of 255 (FF)
4
http://wikistudent.ws/Unisa
main: mov di,buffer
add di,2
mov bl,10
mov al,255 ; FF is the largest number you can use in al
loop1: xor ah,ah ; clear ah (because idiv uses the entire ax)
; not clearing ah gives a division overflow
idiv bl ; divide ax by bl
; result in al, remainder in ah
add ah,30h
mov [di],ah
cmp al,0
je finis
dec di
jmp loop1
int 20h
inputbuffer: db 6;
db 0;
resb 6;
finis: ret
5
http://wikistudent.ws/Unisa
mov ah,10
int 21h
mov dx,inputbuffer
inc dx
inc dx
mov bx,dx
mov al,[inputbuffer + 1]
mov ah,0
add bx,ax
mov al,'$'
mov [bx],al ; now the string with '$' is in dx
mov di,outputbuffer
add di,4
xor bx,bx
mov bx,10
int 20h
int 20h
Notes:
• You can’t read in or print numbers using their actual values - they are
always treated as ASCII strings.
• The largest number that can be displayed is 65535. Because 5 is added to
the input value, the largest number you can enter is 65530.
6
http://wikistudent.ws/Unisa
i to 1000, in increments of i, replacing these multiples of i with ‘0’ (because
multiples of anything cannot be prime!).
The program prints the list of ‘1’s and ‘0’s. A ‘1’ indicates that the number
in that position (starting from position 1, not 0) is prime. If it is ‘0’, the
number in that position is not prime.
org 0x100
bits 16
jmp main
int 20h
main: mov di,buffer ; this will store the output string later
add di,4 ; point to the last space
mov ax,[number] ; the number, and product so far = in dx:ax
mov cx,ax ; used to multiply, with each loop iteration
dec cx ; because you start multiplying by n - 1
int 20h
mov ah,2dh
int 21h
int 20h
Notes:
• You can shorten the program with mov cx,nn and mov dx,nn instead
• Remember: mov ch,10h and mov cl,23h is the same as mov cx,1023h!
input_buffer: db 21
buffer_len: db 00
string: resb 21
mov dx,cr_lf
mov ah,9
int 21h ; move to the next line
loop2: pop ax
8
http://wikistudent.ws/Unisa
mov [di],al ; move one char at a time
inc di ; point to the next space
loop loop2
int 20h
Note: The stack is implicitly used when a procedure is called (with ‘call’).
The address of the instruction following the call is pushed onto the stack and
when the ‘ret’ instruction is executed in the subroutine, it’s popped off.
Remember: when you’re in a subroutine, the return address is always on top of
the stack!
int 20h
• mov dx,buffer and lea dx,[buffer] have the same effect, but these
different instructions may occupy a different number of bytes. You can
always insert ‘nop’ to fill in the gap.
Parameter passing:
There are three ways of passing parameters:
1. Store the parameters in a parameter block in memory
2. Store the parameters in registers before calling the subroutine
3. Push the parameters onto the stack
In this program, the procedure that accepts a string from the keyboard makes
use of the ‘input_buffer’, by storing the length of the string entered, etc.
This buffer is passed as a parameter by the statement ‘mov dx,input_buffer’,
just before the procedure call.
org 0x100
bits 16
jmp main
; parameter block:
input_buffer: db 5 ; max 5 chars
9
http://wikistudent.ws/Unisa
db 0 ; length of string entered
resb 5 ; 5 bytes reserved as input buffer
org 0x100
bits 16
jmp main
11