
;*****************************************************************
;
; Copyright 2002   PIER LUCA MONTESSORO
;
; University of Udine
; ITALY
;
; montessoro@uniud.it
; www.montessoro.it
;
; This file is part of a freeware open source software package.
; It can be freely used (as it is or modified) as long as this
; copyright note is not removed.
;
; SIMCPU: assembly language examples
;
;******************************************************************

; TOUPPER_STRLEN function test program


STACKADDR: word 0F000           ; beginning of stack area
INBUFADDR: word 0E000           ; beginning of input buffer area

;****************************************************************************

; LIBRARY FUNCTIONS


; GETS function: reads a line and stores the string
;                at memory address passed in R1

GETS:      LDBI R0 0A           ; void gets (char *str)
L_GETS:    INB R2 0001
           TSTI 0001     
           JMPNZ L_GETS
           STBR R2 R1    
           SUB R0 R2
           JMPZ END_GETS   
           INC R1
           JMP L_GETS
END_GETS:  LDBI R2 0
           STBR R2 R1    
           RET

; PUTS function: sends to the output device the string 
;                whose address is passed in R1

PUTS:      LDBR R2 R1           ; void puts (char *str)
           JMPZ END_PUTS
L_PUTS:    OUTB R2 0000
           TSTO 0000     
           JMPNZ L_PUTS
           INC R1
           JMP PUTS
END_PUTS:  LDBI R2 0A
LEND_PUTS: OUTB R2 0000
           TSTO 0000     
           JMPNZ LEND_PUTS
           RET

; TOUPPER_STR function: replaces lowecase character in the string with uppercase characters; string address is passed in R1

LOWER_A:        byte 61              ; 61h = 97
UPPER_A:        byte 41              ; 41h = 65
LOWER_Z:        byte 7A              ; 7Ah = 122
UPPER_Z:        byte 5A              ; 5Ah = 90 (unused; see TOLOWER)

TOUPPER_STR:    LDBA R2 LOWER_A      ; void toupper_str (char *s)
                LDBA R3 LOWER_Z
                LDBA R4 UPPER_A

TOUPSTR_L1:     LDBR R5 R1
                JMPZ TOUPSTR_END
                MV R5 R10
                SUB R2 R10
                JMPN TOUPSTR_NOLOW   ; s[i] < 'a'?
                MV R3 R10
                SUB R5 R10
                JMPN TOUPSTR_NOLOW   ; s[i] > 'z'?
                SUB R2 R5
                ADD R4 R5
                STBR R5 R1
TOUPSTR_NOLOW:  INC R1
                JMP TOUPSTR_L1

TOUPSTR_END:    RET

;****************************************************************************

; MAIN PROGRAM

START:     LDWA R15 STACKADDR   ; stack initialization
           SPWR R15             ; NOTE: every function receives input
                                ;       values/addresses in R1, R2, ...
                                ;       and provides output in R0

           LDWA R1 INBUFADDR
           PUSH R1
           CALL GETS
           POP R1

           PUSH R1
           CALL TOUPPER_STR
           POP R1

           PUSH R1
           CALL PUTS
           POP R1

           ; the end
           HLT
