MIPS: Saving a String to register(s)?

In summary, the conversation is about a student trying to write a MIPS program that converts letters to a phone number. They have two questions: 1) how to save the string into registers, and 2) strategies for converting the string into numbers. The expert suggests storing the string in memory and using ASCII codes and conditional branches for conversion. The conversation also includes some sample code for validation and conversion.
  • #1
pags920
21
0

Homework Statement



I am currently working on a MIPS program that is supposed to convert letters, either lowercase or uppercase, to a phone number.

Two questions:

1. With the user entering a string, how do I go about in saving the string into the registers? Or would memory be a more convenient choice?

2. Does anyone have any strategies or hints as to how I can go about in converting the string into numbers? One thought I had was to use the ASCII chart with conditional branches, but that would involve many lines of code.

Below is only the Main subroutine.

The Attempt at a Solution


Code:
#INITIALIZATION
.data
msg0:	.asciiz "Please enter the phone number, expressed in letters, that you wish to convert.\n"
msg1:	.asciiz "You have entered: \n"
msg2:	.asciiz "The phone number you entered is invalid.\n"
msg3:	.asciiz "The phone number you entered is valid.\n"
msg4:	.asciiz "Your phone number, converted from letters to numbers, is \n"
nl:	.asciiz "\n"

str:	.space 7

#############################################
# 		Main
############################################# 
.text
.globl main
main:
	la $a0, msg0
	li $v0, 4
	syscall

	la $a0, str
	li $a1, 8
	li $v0, 8
	syscall

	la $a0, nl
	li $v0, 4
	syscall

	la $a0, msg1
	li $v0, 4
	syscall

	la $a0, str
	li $v0, 4
	syscall
 
Physics news on Phys.org
  • #2
pags920 said:

Homework Statement



I am currently working on a MIPS program that is supposed to convert letters, either lowercase or uppercase, to a phone number.
Caveat: I haven't done any programming on MIPS machines, but I have done a fair amount of assembly programming using Intel x86 assembly and some on Motorola 68000.
pags920 said:
Two questions:

1. With the user entering a string, how do I go about in saving the string into the registers? Or would memory be a more convenient choice?
I would put the phone number string in memory. Your program would allocate space for the input string and output string of digits in the data segment with the other variables, and your input routine would take input from the user and store it in the input string variable. Another part of your program (a different routine is my recommendation) would store the converted string of digits in the other variable. If you're working with local (not long-distance) US phone numbers, seven bytes each would suffice, and that seems to be what you're doing in the code you show.
pags920 said:
2. Does anyone have any strategies or hints as to how I can go about in converting the string into numbers? One thought I had was to use the ASCII chart with conditional branches, but that would involve many lines of code.
Lets' face it - you're writing assembly code, so most things you do are going to require many lines of code.

As far as using ASCII codes for the characters, you should do this for sanity checking - to make sure that your input values are actually characters, and to convert any lower-case letters to upper case. The ASCII codes for A through Z are 65 through 90 (0x41 through 0x5A in hex) and for the lower-case letters, they are 97 through 122 (0x61 thorough 0x7A). You will need to use conditional branches to convert the characters to the corresponding numbers.

You'll need to make some assumptions as to how the phone keypad is laid out. The old rotary-dial phones didn't have Q or Z, and laid out the remaining 24 letters equally on the numbers 2 through 9. My Panasonic wireless home phone and Nokia cell have a different arrangement, with PQRS on the 7 key and WXYZ on the 9 key, and three letters each on the other seven keys.

The logic (shown here roughly in C) would go something like this, assuming you have already validated your input data and converted lower-case letters to upper case:
Code:
for (i = 0; i < 7 ; ++i)
{
   if(char == 'A' OR char == 'B' OR char == 'C') outString[i] = '2';
   else if (char == 'D' OR char == 'E' OR char == 'F') outString[i] = '3';
   else if (char == 'G' OR char == 'H' OR char == 'I') outString[i] = '4';
   .
   .
   .
}
pags920 said:
Below is only the Main subroutine.

The Attempt at a Solution


Code:
#INITIALIZATION
.data
msg0:	.asciiz "Please enter the phone number, expressed in letters, that you wish to convert.\n"
msg1:	.asciiz "You have entered: \n"
msg2:	.asciiz "The phone number you entered is invalid.\n"
msg3:	.asciiz "The phone number you entered is valid.\n"
msg4:	.asciiz "Your phone number, converted from letters to numbers, is \n"
nl:	.asciiz "\n"

str:	.space 7

#############################################
# 		Main
############################################# 
.text
.globl main
main:
	la $a0, msg0
	li $v0, 4
	syscall

	la $a0, str
	li $a1, 8
	li $v0, 8
	syscall

	la $a0, nl
	li $v0, 4
	syscall

	la $a0, msg1
	li $v0, 4
	syscall

	la $a0, str
	li $v0, 4
	syscall
 

What is MIPS?

MIPS (Microprocessor without Interlocked Pipeline Stages) is a reduced instruction set computer (RISC) architecture that is commonly used in embedded systems and gaming consoles. It is designed to be efficient and fast, using a limited number of simple instructions.

How do I save a string to a register in MIPS?

To save a string to a register in MIPS, you will need to use the la (load address) instruction. This instruction takes two parameters, the first being the register where you want to store the string and the second being the address of the string. For example, if you want to save the string "Hello, world!" to $s0, you would use the instruction la $s0, string where string is the address of the string in memory.

Can I save more than one string to a register in MIPS?

No, a register in MIPS can only hold one value at a time. If you need to store multiple strings, you will need to use different registers for each string or use memory to store the strings.

How do I access the string stored in a register in MIPS?

To access the string stored in a register in MIPS, you will need to use the lw (load word) instruction. This instruction takes two parameters, the first being the register where the string is stored and the second being the offset from that register. For example, if you have the string "Hello, world!" stored in $s0 and you want to access the first character, you would use the instruction lw $t0, 0($s0).

Can I modify a string that is stored in a register in MIPS?

No, strings in MIPS are stored in memory and cannot be modified directly in a register. To modify a string, you will need to access the string in memory and then store the modified string back into the memory location. However, you can use arithmetic and logical operations on the string in a register to manipulate it before storing it back in memory.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
4
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
4K
  • Engineering and Comp Sci Homework Help
Replies
22
Views
4K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
4K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
6K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
9K
  • Engineering and Comp Sci Homework Help
Replies
12
Views
10K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
4K
Back
Top