Write a MIPS program for encoding and decoding morse code using
memory-mapped I/O. You will use a simply written version of morse
code where dots are represented by a period (ASCII 4610), dashes by
a hyphen (ASCII 4510), and letter breaks by a space (ASCII 3210),
and word breaks by a slash (ASCII 4710). For example, the string
“hello world” would be represented in morse code as “.... . .-..
.-.. --- / .-- --- .-. .-.. - ..”. This version of morse code will
only support encoding lower case letters and spaces. Beginning with
the provided code for this question, implement the main, decode,
and encode as described below.
The main procedure of your program should consist of a loop that
asks the user to enter a single character, 'd' for decode or 'e'
for encode. The character should be echoed to the console, followed
by a newline character. Next, if the uses pressed 'd', invoke your
decode procedure, described below. If the user pressed 'e', invoke
your encode procedure, described below. If the user presses any
other character, terminate. Once decode or encode returns, loop
again.
The decode procedure should accept as input a series of morse
code characters. Each time a period, hyphen, or slash is input, it
should be placed into a buffer (use the space declared in the .data
section with the label decodeBuffer). When the space key is
pressed, add null terminator to the buffer (do not store the
space). The address of the buffer should then be passed to the
provided decodeChar function, which will return the actual
character to print. Finally, the decoded character should be output
to the console. If the decodeChar procedure returns 0, the input
was invalid, and nothing should be output. The procedure should
continue accepting morse code input and echoing the
decoded characters to the console until the enter key is
pressed, then echo a newline character to the console and
return.
The encode procedure should accept as input a series of lower
case letters and spaces. Each time a character is input, its value
should be passed to the provided encodeChar procedure, which will
return the address of a string containing the morse code encoding
of that character, which should be output. If encodeChar returns 0,
the input was invalid, and nothing should be output. The procedure
should continue accepting characters as input and echoing their
morse code encoding to the console until the enter key is pressed,
then echo a newline character to the console and return.
In addition to implementing main, encode, and decode, you may
add other procedure if needed. However, do not modify or add to the
.data section, and do not modify the provided procedures encodeChar
and decodeChar. All procedures you implement must use the
“standard” conventions discussed in class for passing parameters
and returning results. Do not use any syscall instructions, except
to terminate at the end of your program.
This is the provide code:
.data
initialPrompt: .asciiz "Would you like to encode ('e') or decode
('d')? "
decodeTree: .word _e _t 0; _e: .word _i _a 'e'; _t: .word _n _m
't'; _i: .word _s _u 'i'; _a: .word _r _w 'a'; _n: .word _d _k 'n';
_m: .word _g _o 'm'; _s: .word _h _v 's'; _u: .word _f 0 'u'; _r:
.word _l 0 'r'; _w: .word _p _j 'w'; _d: .word _b _x 'd'; _k: .word
_c _y 'k'; _g: .word _z _q 'g'; _o: .word 0 0 'o'; _h: .word 0 0
'h'; _v: .word 0 0 'v'; _f: .word 0 0 'f'; _l: .word 0 0 'l'; _p:
.word 0 0 'p'; _j: .word 0 0 'j'; _b: .word 0 0 'b'; _x: .word 0 0
'x'; _c: .word 0 0 'c'; _y: .word 0 0 'y'; _z: .word 0 0 'z'; _q:
.word 0 0 'q';
encodeArray: .word _ea _eb _ec _ed _ee _ef _eg _eh _ei _ej _ek _el
_em _en _eo _ep _eq _er _es _et _eu _ev _ew _ex _ey _ez; _ea:
.asciiz ".-"; _eb: .asciiz "-..."; _ec: .asciiz "-.-."; _ed:
.asciiz "-.."; _ee: .asciiz "."; _ef: .asciiz "..-."; _eg: .asciiz
"--."; _eh: .asciiz "...."; _ei: .asciiz ".."; _ej: .asciiz ".---";
_ek: .asciiz "-.-"; _el: .asciiz ".-.."; _em: .asciiz "--"; _en:
.asciiz "-."; _eo: .asciiz "---"; _ep: .asciiz ".--."; _eq: .asciiz
"--.-"; _er: .asciiz ".-."; _es: .asciiz "..."; _et: .asciiz "-";
_eu: .asciiz "..-"; _ev: .asciiz "...-"; _ew: .asciiz ".--"; _ex:
.asciiz "-..-"; _ey: .asciiz "-.--"; _ez: .asciiz "--..";
decodeBuffer: .space 5
slash: .asciiz "/"
.text
# DO NOT ADD OR MODIFY ABOVE THIS LINE
main:
terminate:
li $v0, 10
syscall
# Reads input from the console one character at a time. Each
time a character
# is typed, the corresponding morse code symbols are output to the
console,
# followed by a space. If enter is pressed, it is echoed to the
console, and
# the procedure returns. If a character cannot be encoded, it is
ignored.
encode:
jr $ra
# Reads input from the console in morse code format. The input
is placed in a
# buffer. When space is pressed, the buffer is zero-terminated and
passed to
# decodeChar be decoded. The decoded character is printed to the
console. If
# enter is pressed, it is echoed to the console, and the procedure
returns.
# If the character cannot be decoded, it is ignored.
decode:
jr $ra
# DO NOT ADD OR MODIFY BELOW THIS LINE
# Decodes a series of morse code symbols and returns a single
character
# arg: $a0: address of a (zero terminated) string containing morse
code symbols
# ret: $v0: the decoded character; 0 on error
decodeChar:
lb $t0, 0($a0)
beq $t0, '/', decodeReturnSpace
la $t0, decodeTree
decodeCharLoop:
beq $t0, $0, decodeReturnNull
lb $t1, 0($a0)
addi $a0, $a0, 1
beqz $t1, decodeReturnLetter
beq $t1, '.', decodeDot
beq $t1, '-', decodeDash
b decodeReturnNull
decodeDot:
lw $t0, 0($t0)
b decodeCharLoop
decodeDash:
lw $t0, 4($t0)
b decodeCharLoop
decodeReturnSpace:
li $v0, 32
jr $ra
decodeReturnLetter:
lw $v0, 8($t0)
jr $ra
decodeReturnNull:
li $v0, 0
jr $ra
# Gives the encoding of a character in morse code
# arg: $a0: the character to encode (lower case letters and space
only)
# ret: $v0: the address of a string containing the morse code
encode; 0 on error
encodeChar:
beq $a0, ' ', encodeCharRetSlash
blt $a0, 'a', encodeCharRetError
bgt $a0, 'z', encodeCharRetError
addi $a0, $a0, -97
la $t0, encodeArray
sll $a0, $a0, 2
add $t0, $t0, $a0
lw $v0, 0($t0)
jr $ra
encodeCharRetError:
li $v0, 0
jr $ra
encodeCharRetSlash:
la $v0, slash
jr $ra
Write a MIPS program for encoding and decoding morse code using memory-mapped I/O. You will use a simply written version
-
- Site Admin
- Posts: 899603
- Joined: Mon Aug 02, 2021 8:13 am