Converting Numbers/Letters to ASCII Codes

  • Thread starter Thread starter jjlee2
  • Start date Start date
Join the discussion
Ask a follow-up here, or get your own question answered by working scientists, mathematicians and engineers — people, not an autocomplete.
Real named experts · corrections over time · the nuance an AI answer skips
5 replies · 6K views
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
 
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?