Converting Numbers/Letters to ASCII Codes

  • Thread starter Thread starter jjlee2
  • Start date Start date
Click For Summary
The discussion revolves around a program that converts characters to ASCII values but fails to handle more than four inputs, resulting in zeros for subsequent entries. The code snippet provided shows the program structure, including data declarations and the main routine. Users suggest using a debugger, specifically PCSpim, to step through the program and observe register and memory values. It is noted that the program stores the first four characters correctly but fails to store additional characters, leading to confusion about memory allocation and register use. Participants emphasize the importance of adding comments to the code for clarity and understanding. Questions arise regarding the purpose of certain variables and the logic behind decrementing the input counter. Overall, the discussion highlights debugging techniques and the need for clearer code documentation to resolve the issue.
jjlee2
Messages
3
Reaction score
0
hi i have a program that reads in numbers or letters and converts them to ascii but it will only work for up to 4 items and after that i just get zeros, here's the code for the program


.data

x: .byte 00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00
y: .word 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
total: .word 0

str1: .asciiz "
Enter a character "
newln: .asciiz "
"
str2: .asciiz "Character Decimal Value "
str3: .asciiz "Enter total number of values: "
space: .asciiz " "

.text
.globl main

main: la $s0,x
la $s1,y

li $v0,4
la $a0, str3
syscall

li $v0,5
syscall

sw $v0,total

lw $s2, total

loop1: slti $t0,$s2,1
bne $t0,$zero,out

li $v0,4
la $a0,str1
syscall

li $v0,12
syscall

sb $v0,0($s0)

addi $s0,$s0,1
addi $s2,$s2,-1

j loop1

out: lw $s2,total
la $s0,x
lw $s3,0($s0)

loop2:
slti $t0,$s2,1
bne $t0,$zero,done

andi $t1,$s3,255
sw $t1,0($s1)
srl $s3,$s3,8

addi $s1,$s1,4
addi $s2,$s2,-1

j loop2

done: la $s0,x
la $s1,y
lw $s2, total

li $v0,4
la $a0,newln
syscall

li $v0,4
la $a0,newln
syscall

li $v0, 4
la $a0, str2
syscall


loop3: slti $t0,$s2,1
bne $t0,$zero,fin

li $v0,4
la $a0,newln
syscall

li $v0,11
lb $a0,0($s0)
syscall

li $v0,4
la $a0,space
syscall

li $v0,1
lw $a0,0($s1)
syscall

addi $s0,$s0,1
addi $s1,$s1,4
addi $s2,$s2,-1

j loop3

fin: jr $ra


thanks in advance for any help with this problem
 
Technology news on Phys.org
Do you have any kind of debugger to work with? It would be very helpful to be able to single-step through your program and see what values are in memory and in the various registers.

Also, you don't have a single comment in your program. Assembly programs by their very nature, should have lots of comments.
 
yes I've been using pcspim to run it, and that has a single-step function
 
Does the debugger in PCSpim show you the values of the registers and memory? If so, you could single-step through your program and see what happens after you enter the fourth character.
 
i have done that and it starts putting the 5th through 8th in the next register the first four numbers are stored like so 0x64636261 then the fifth and sixth are stored in the next register like so 0x00006665. then it separates the values like so 0x00000061 0x00000062 0x00000063 0x00000064 0x00000000 0x00000000 and doesn't put the 65 and 66 into the last 2
 
Code:
main: la $s0,x
la $s1,y

; prompt user to enter total number of values
li $v0,4
la $a0, str3
syscall

; read integer from keyboard
li $v0,5
syscall

; store word in $v0 at address total
sw $v0,total

; load value at total into $s2
lw $s2, total

loop1: slti $t0,$s2,1
bne $t0,$zero,out

; prompt user to enter a character
li $v0,4
la $a0,str1
syscall

; read a character from the keyboard
li $v0,12
syscall

; store the character at address $s0 + 0
sb $v0,0($s0)

; increment $s0 pointer 
addi $s0,$s0,1

; decrement $s2 pointer - ?
addi $s2,$s2,-1

j loop1
I have taken the first part of your main routine and added comments to help me understand what it is doing. I used ; to start a comment, which might not be the correct MIPS syntax. It would be an excellent idea for you to correct any comments that I have that are incorrect, and put in comments for the rest of your program. That would help both of us understand what is going on.

The first thing your program prompts for is an integer, which represents the number of characters you are going to enter. What are you entering for this value?

jjlee2 said:
i have done that and it starts putting the 5th through 8th in the next register the first four numbers are stored like so 0x64636261 then the fifth and sixth are stored in the next register like so 0x00006665. then it separates the values like so 0x00000061 0x00000062 0x00000063 0x00000064 0x00000000 0x00000000 and doesn't put the 65 and 66 into the last 2
You aren't being very clear here about what you are doing. When you say "next register" which one do you mean? I'm not there looking at the debugger with you, so you need to be very specific about what you're seeing.

It looks like to me you entered 1, 2, 3, 4, 5, and 6. The ASCII values of these characters are 0x61, 0x62, 0x63, on up to 0x66. Each of these numbers will fit into a byte. Is the idea that the characters you enter go into the x storage, and their ascii codes go into the y storage? I don't understand what those two variables are used for.

Near the end of the block of code I copied, you have addi $s2,$s2,-1. Why are you doing this?
 
Learn If you want to write code for Python Machine learning, AI Statistics/data analysis Scientific research Web application servers Some microcontrollers JavaScript/Node JS/TypeScript Web sites Web application servers C# Games (Unity) Consumer applications (Windows) Business applications C++ Games (Unreal Engine) Operating systems, device drivers Microcontrollers/embedded systems Consumer applications (Linux) Some more tips: Do not learn C++ (or any other dialect of C) as a...

Similar threads

  • · Replies 4 ·
Replies
4
Views
1K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 5 ·
Replies
5
Views
10K
  • · Replies 4 ·
Replies
4
Views
5K
  • · Replies 1 ·
Replies
1
Views
8K
  • · Replies 1 ·
Replies
1
Views
10K
Replies
1
Views
2K
  • · Replies 4 ·
Replies
4
Views
7K
  • · Replies 22 ·
Replies
22
Views
5K
  • · Replies 1 ·
Replies
1
Views
6K