Friday, March 13, 2020

Draw rectangle in MIPS

This is a sample MIPS assembler code to draw a rectangle using any char. The char and the dimensions are given by the user. The code is fully commented.

Launch EzMIPS, copy the following MIPS code and paste it into EzMIPS, the MIPS assembler editor & simulator. Assemble, Run.


# -------- Draw rectangle with a char given by the user ------- #


# ------------------------ Data section ----------------------- #
.data

szIntro:       .asciiz  "....... Draw Rectangle .......\n\n"

szWidthError:  .asciiz "Error: Width out of bounds"
szLengthError: .asciiz "Error: Length out of bounds"

szWidth:       .asciiz "Width  (max=20): "
szLength:      .asciiz "Length (max=20): "
szEnterChar:   .asciiz "Char to draw   : "
szLF:          .asciiz "\n"

# ----------------------- Code section ------------------------ #

.text
.globl main

main:

# ............................................................. #
  
    # Print "...... Draw Rectangle ......\n\n"
    la $a0, szIntro
    li $v0, 4
    syscall

# ............................................................. #
  
    # Print "Rectangle\'s width  (max=20): "
    la $a0, szWidth
    li $v0, 4
    syscall

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

    li $v0, 5                  # Read the integer (width of rectangle)
    syscall
    move $t3, $v0              # Keep the rectangle's width in $t3

# ............................................................. #
  
    li $at, 20
    bgt $t3, $at, width_error  # If greater than 20
    li $at, 0
    ble $t3, $at, width_error  # If less than 0

# .............................................................. #
  
    # Print "Rectangle\'s length (max=20): "
    la $a0, szLength
    li $v0, 4
    syscall

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

    li $v0, 5                  # Read the integer (length of rectangle)
    syscall
    move $t1, $v0              # Keep the rectangle's length in $t1

# ............................................................. #
  
    li $at, 20
    bgt $t1, $at, length_error # If greater than 20
    li $at, 0
    ble $t1, $at, length_error # If less than 0

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

    la $a0, szEnterChar        # Message to print character
    li $v0, 4
    syscall

# ............................................................. #
  
    li $v0, 12                 # Read char and move it to $s0
    syscall
    move $s0, $v0              # char to draw is held by $s0

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

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

    addi $t2, $zero, 0         # $t2 = counter = 0

# ............................................................. #
next_line:

    jal print_line_of_chars  
    addi $t2, $zero, 0         # reset counter
    addi $t1, $t1, -1

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

    bne $t1, $zero, next_line
    j exit

# ............................................................. #
print_line_of_chars:           # Print line of chars($t3 times)
    addi $t2, $t2, 1
  
    move $a0, $s0              # Copy character to print in $a0
    li $v0, 11
    syscall                    # Print it!
  
    blt $t2, $t3, print_line_of_chars

    jr $ra                     # return to caller (next_line)

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

width_error:
    #print "Error: Width out of bounds"
    la $a0, szWidthError
    li $v0, 4
    syscall

    # Exit
    li $v0, 10
    syscall

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

length_error:
    #print "Error: Length out of bounds"
    la $a0, szLengthError
    li $v0, 4
    syscall

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

exit:                          # 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