Draw grid of boxes in MIPS assembly language

Draw grid of boxes in MIPS assembly language

Create the “class” (.struct) box

  • create two instances of box
    • single line
    • double line
  • write void box.draw(int x:a0, int y:a1, int width:a2, int height:a3, box * b:s0)
  • write a main program to ask the user for the difficulty level , input their answer, clear the screen and draw the boxes (use the double outline).
  • write a program (any language) to process the word list in course material, and filter out words that are longer than 3 letters…(you may leave or reject shorter words) Be sure to preserve Capitalization of words as some may be proper nouns or pronouns ( “I” “Jay” “Ron” “Dan”).
    Write your harvested word list back to a new file, one word per line.
  • submit your .asm and your word list file (.txt)

Solution

filter.cpp 

#include<iostream>

#include<fstream>

#include<string>

#include<cstdlib>

int main() {

std::cout<<“Enter the words filename: “;

std::string l;

std::cin>>l;

std::ifstream f(l.c_str());

if(!f.is_open())

std::cout<<“File not found!\n”,exit(1);

std::ofstream o(“filtered.txt”);

while(getline(f,l)) {

std::size_t y=l.find_last_not_of(“\r\n”);

(y!=std::string::npos)? l.erase(y+1),((l[0]&&l.length()<=3)? o<<l<<std::endl,1 : 2) : 3;

}

o.close(),f.close();

return 0;

} 

boxes.asm

.data

prompt: .asciiz “Enter difficulty level: ”

errmsg: .asciiz “Invalid level, must be between 1 and 6.\n”

sz:     .byte 1,2,2,3,3,4

.data

box: .struct

ctl:    .byte 0

ctr:    .byte 0

cbl:    .byte 0

cbr:    .byte 0

ch:     .byte 0

cv:     .byte 0

.data

box1: .byte 218,191,192,217,196,179

box2: .byte 201,187,200,188,205,186

.code

.globl     main

main:

la      $a0,prompt

syscall $print_string

syscall $read_int

blt $v0,1,error

bgt $v0,6,error

addi $t0,$0,25

addi $a0,$0,10

1:  syscall $print_char

addi $t0,$t0,-1

bnez $t0,1b

add $t2,$v0,-1

la  $t1,sz

add $t1,$t1,$t2

lb  $t2,($t1)

add $v0,$v0,1

sub  $t4,$v0,$t2

addi $t0,$0,1

sll  $t4,$t0,$t4

addi $t0,$0,1

sll  $t3,$t0,$t2

1:  add  $t5,$0,$t3

2:  addi $a2,$0,5

addi $a3,$0,3

addi $a0,$t5,-1

mult $a0,$a2

mflo $a0

addi $a1,$t4,-1

mult $a1,$a3

mflo $a1

la   $s0,box2

jalbox_draw

addi $t5,$t5,-1

bnez $t5,2b

addi $t4,$t4,-1

bnez $t4,1b

syscall    $exit

error:

la $a0,errmsg

syscall $print_string

b   main

# voidbox.draw(int x:a0, int y:a1, int width:a2, int height:a3, box * b:s0)

box_draw:

add $t0,$a0,$0

add $t1,$a1,$0

add $a2,$a2,-1

add $a3,$a3,-1

syscall    $xy

lb  $a0,box.ctl($s0)

syscall $print_char

add $a0,$t0,$a2

add $a1,$t1,$0

syscall    $xy

lb  $a0,box.ctr($s0)

syscall $print_char

add $a0,$t0,$0

add $a1,$t1,$a3

syscall    $xy

lb  $a0,box.cbl($s0)

syscall $print_char

add $a0,$t0,$a2

add $a1,$t1,$a3

syscall    $xy

lb  $a0,box.cbr($s0)

syscall $print_char

addi $t2,$0,1

forw:

add $a0,$t0,$t2

add $a1,$t1,$0

syscall    $xy

lb  $a0,box.ch($s0)

syscall $print_char

add $a0,$t0,$t2

add $a1,$t1,$a3

syscall    $xy

lb  $a0,box.ch($s0)

syscall $print_char

addi $t2,$t2,1

blt  $t2,$a2,forw

addi $t2,$0,1

forh:

add $a0,$t0,$0

add $a1,$t1,$t2

syscall    $xy

lb  $a0,box.cv($s0)

syscall $print_char

add $a0,$t0,$a2

add $a1,$t1,$t2

syscall    $xy

lb  $a0,box.cv($s0)

syscall $print_char

addi $t2,$t2,1

blt  $t2,$a3,forh

jr $ra