
.macro EXIT_
        li   a0, 0 # return code 0
        li   a7, 93 # sys_exit
        ecall # system call
.endm

.macro GETCHAR
        add  sp, sp, -8
        sd   s3, (sp)
        add  sp, sp, -8
        sd   s8, (sp)
        add  sp, sp, -8
        sd   s10, (sp)
        li   a0, 0 # stdin
        lui  a1, %hi(in_ch)
        addi a1, a1, %lo(in_ch)
        li   a2, 1 # read one character
        li   a7, 63 # sys_read
        ecall # system call
        bne  a0, zero, .L1 # returned size == 0
        li   s0, -1 # eof
        j    .L0
.L1:
        lw   s0, (a1) # returned character in s0
.L0:
        ld   s10, (sp)
        add  sp, sp, 8
        ld   s8, (sp)
        add  sp, sp, 8
        ld   s3, (sp)
        add  sp, sp, 8
.endm

.macro PUTCHAR
        add  sp, sp, -8
        sd   s3, (sp)
        add  sp, sp, -8
        sd   s8, (sp)
        add  sp, sp, -8
        sd   s10, (sp)
        li   a0, 1 # stdout
        lui  a1, %hi(out_ch)
        addi a1, a1, %lo(out_ch)
        sw   s0, (a1) # s0 has character to print
        li   a2, 1 # write one character
        li   a3, 0
        li   a7, 64 # sys_write
        ecall # system call
        ld   s10, (sp)
        add  sp, sp, 8
        ld   s8, (sp)
        add  sp, sp, 8
        ld   s3, (sp)
        add  sp, sp, 8
.endm
