; A program to demonstrate array use
;===================================
; useful values :)
element_size equ 012h
element_number equ 005h
array_size equ 060h ; >= (element_size * element_number)
.model 386 ; tell masm the model we use
; (my MASM complains if you dont
; include a model...)
;=================================
.stack
db 100h dup(?) ; put a stack here to stop
; MASM complaining...
;=================================
.data
; Needed for better text i/o
message db 'Enter string >$' ; Prompt message
newline db 0dh,0ah,'$' ; New line (!)
; Buffer used for input and output
buffer db 11h ; Max buffer size (element_size - 1)
db ? ; Actual buffer size
db 11h dup(?) ; Make buffer
; Array to hold strings:
array db array_size dup('*')
db '$'
; used for accessing different elements of the array
array_position dw ?
;=================================
.code
; Set up segment registers
mov ax,@data
mov ds,ax
mov es,ax
;====================================================
; This first loop gets input from the user and
; converts the result from the interrupt into the
; 0-terminated string stored in the array.
;====================================================
lea di,array
mov [array_position],di ; save array position
mov cx,element_number
input_loop:
push cx ; save value for end of loop
; Output prompt
mov ah,09h
lea dx,newline ; Leave a line before prompt
int 21h
mov ah,09h
lea dx,message
int 21h
; Get input into buffer
mov ah,0ah
lea dx,buffer
int 21h
; transfer input from buffer into array
lea si,buffer
inc si
sub cx,cx
mov cl,[si] ; Get size of string
inc si
string_transfer:
lodsb ; get byte from buffer into al
mov [di],al ; move byte from al into array
inc di ; update string position
loop string_transfer
mov al,00h
mov [di],al
pop cx ; restore value...
mov di,[array_position]
add di,element_size ; ...update array position...
mov [array_position],di
loop input_loop ; ...and loop!
;====================================================
; This next loop converts all the strings in the
; array into a form in the buffer for outputing
; to the screen.
;====================================================
lea si,array
mov [array_position],si ; save array position
mov cx,element_number
output_loop:
push cx ; save value for end of loop
; transfer input from array into buffer
lea di,buffer
string_transfer2:
lodsb ; get byte from string into al
or al,al ; check if it is zero (ie end string)
jz end_string_transfer2
mov [di],al ; move byte from al into buffer
inc di ; update string position
jmp string_transfer2
end_string_transfer2:
mov al,'$' ; required to show end of string
mov [di],al
mov ah,09h
lea dx,newline ; Print on next line
int 21h
lea dx,buffer ; Print contents of buffer
int 21h
pop cx ; restore cx value...
mov si,[array_position]
add si,element_size ; ...update array position...
mov [array_position],si
loop output_loop ; ...and loop!
;=================================
mov ah,09h
lea dx,newline ; Next line...
int 21h
; I have included this here to show you what
; the array looks like in memory.
; You will understand when you run the program
; :)
mov ah,09h
lea dx,array
int 21h
mov ah,09h
lea dx,newline ; Next line...
int 21h
; Terminate program
mov ax,4c00h
int 21h
end