;  A 16-bit DOS HelloWorld program originally by RedOx.  Produces a small model .EXE executable.

; To assemble and link from within WinAsm Studio, you must have a special 16-bit linker, 
; such as the one in the archive at this URL- http://win32assembly.online.fr/files/Lnk563.exe
; Run the archive to unpack Link.exe, rename the Link.exe file to Link16.exe and copy it
; into the \masm32\bin folder.

.MODEL	small
.stack	100h

.data 
	msg		db 13,10,13,10,13,10,"Fine del programma. Premi un tasto per uscire!",13,10,"$"; The string must end with a $
    vett1   db 1,2,55,77,147,5,6,9,9,5
    vett2   db 3,5,6,7,2,5,9,5,2,3
    vett3   db 0,0,0,0,0,0,0,0,0,0
    n       db 3
    a       db 'A'
    b       db 'B'
    elem	db "Elemento ",'$'
    uguale  db " = ", '$'

.code

; output
;   Invia un carattere sullo standard output (di solito il video)
;   Parametri in ingresso: AL
output proc
	push ax
	push dx
	mov  ah, 2
	mov  dl, al
	int  21h
	pop  dx
	pop  ax
	Ret
output endp

; print
;  Stampa la stringa presente alla locazione di memoria DS:DX;
;  tale stringa deve terminare con il segno "$".
;  Parametri in ingresso: DS:DX
;  Nessun parametro in uscita.
print proc
	push ax
	mov  ah,9	; Function 09h in AH means "WRITE STRING TO STANDARD OUTPUT"	
	int  21h	; Call the DOS interrupt (DOS function call)
	pop  ax
	ret
print endp

;  Converte il numero presente nel registro AL
;  nella corrispondente stringa ASCII
convertiALinStringaASCII proc
    mov ah, 0 ; AX = AH AL = 0 AL
    mov bl, 100
    div bl	; AL = AX / BL = centinaia, AH = resto = decine 
    add al, '0'
    call output
    mov al, ah
    mov ah, 0
    mov bl, 10
    div bl  ; AL = AX / BL = decine, AH = resto = unita`
    add al, '0'
    call output
    mov al, ah
    add al, '0'
    call output   
	Ret
convertiALinStringaASCII EndP

stampaVETT1 proc
	mov cl, 10
	mov si, 0
cicloStampaVETT1:
	;;; stampa la stringa "Elemento "
	mov dx, offset elem ; Carica in dx l'offset della var. elem
	call print
	;;; stampa indice vettore
	mov ax, si
	add al, '0'
	call output
	;;; stampa ' = '
	mov dx, offset uguale
	call print
	;;; stampa contenuto AL (AL e` un numero tra 0 e 255)
	mov al, VETT1[si]
	call convertiALinStringaASCII ; <----
	; stampa il carattere di 'INVIO'
	mov al, 13	; carattere di 'ritorno carello'
	call output
	mov al, 10  ; carattere di 'nuova linea'
	call output
	; fine del ciclo
	inc si
	dec cl
	jnz cicloStampaVETT1
	Ret
stampaVETT1 EndP

start:
	mov		ax,@data		; Get the address of the data segment
	mov		ds,ax			; Set the DS segment
	;;; Inizio Esercizo
	
	;; Stampa le 3 lettere di esempio
	mov al, a
	call output
	mov al, b
	call output
	mov al, 'C'
	call output
	mov al, 13
	call output
	mov al, 10
	call output

	
	mov cl, n
	mov si, 0
     
 ciclo:
 	mov al, vett1[si]
 	add al, vett2[si]
 	mov vett3[si], al
 	inc si
 	dec cl
 	jnz ciclo	
	
	call stampaVETT1
	
	;;; Fine Esercizio
	mov		dx,offset msg	; Get the address of our message in the DX
	mov		ah,9			; Function 09h in AH means "WRITE STRING TO STANDARD OUTPUT"
	int		21h				; Call the DOS interrupt (DOS function call)
	
	mov		ax,0C07h		; Function 0Ch = "FLUSH BUFFER AND READ STANDARD INPUT"
	int		21h				; Waits for a key to be pressed.
	
	mov		ax, 4C00h		; the exit fuction  [4C+no error (00)]
	int		21h				; call DOS interrupt 21h
end start