Monday, March 9, 2020

ASCII Table in MIPS

This is a sample MIPS assembler code to store decimal numbers from 1 - 126 in an array, retrieve one by one and print them as characters. In other words, it will print the ASCII table (including the non-printable characters due to the specific feature of HiEdit control). Full explanation in the comments.

Launch EzMIPS, copy the following MIPS code and paste it into EzMIPS. Assemble, Run.


# -------------------- Print ASCII table ----------------------- #

# 1. store numbers 1 - 126  in a byte array of 126 positions
# 2. read each array position and print the byte stored as a char

.data
szASCIITable: .asciiz "ASCII Table\n-----------\n"
Array:        .space  126

szTAB:        .asciiz "\t"
szLF:         .asciiz "\n"
# -------------------------------------------------------------- #

.text
main:

# ........................ initialize ......................... #

    ori $t2, $zero, 126 # $t2 = number of entries in ASCII table
    la $t1, Array       # $t1 = address of the Array
    li $t0, 1           # $t0 = counter / symbol numeric

# .............. store all symbols in the array ............... #

loop:
    sb $t0, 0($t1)      # store byte in Array
    addi $t0, $t0, 1    # $t0 = next symbol
    addi $t1, $t1, 1    # $t1 = address of next element in Array
    ble $t0, $t2, loop  # if $t0 <= $t2, store next symbol

# ............ print "ASCII Table\n-----------\n" ............. #

    li $v0, 4           # syscall code to print string
    la $a0, szASCIITable
    syscall             # print it!

# .............................................................. #

    la $t1, Array       # $t1 = address of the Array
    li $t0, 1           # $t0 <- 1 (loop counter)

print:
# .............................................................. #

    li $v0, 1           # syscall code to print a char 
    move $a0, $t0
    syscall             # print the symbol

# .............................................................. #

    li $v0, 4          # syscall code to print tab
    la $a0, szTAB
    syscall 

# .............................................................. #

    lbu $a0, 0($t1)     # $a0 <- symbol from array
    li $v0, 11          # syscall code to print a char
    syscall             # print the symbol

# .............................................................. #

    li $v0, 4          # syscall code to print new line
    la $a0, szLF
    syscall 

# .............................................................. #

    addi $t1, $t1, 1    # $t1 = address of next symbol in Array
    addi $t0, $t0, 1    # increment loop counter
    ble $t0, $t2, print # if more symbols to print: keep looping        

# ........................ program exit ........................ #

    li $v0, 10
    syscall

# -------------------------------------------------------------- #


Please let me know of any suggestions or bugs regarding the code above.

Regards,

Antonis

No comments:

Post a Comment