Converting Numbers/Letters to ASCII Codes

  • Thread starter jjlee2
  • Start date
In summary, the conversation involves a program that reads in numbers or letters and converts them to ASCII, but it only works for up to four items and then outputs zeros. There is a lack of comments in the program and it would be helpful to have a debugger to single-step through the program. The program prompts the user to enter the total number of values, then reads in an integer and stores it at the address "total". It then loads the value at "total" into $s2. The loop1 block prompts the user to enter a character, reads it in, and stores it at the address $s0 + 0. It then increments $s0 and decrements $s2. It is unclear what the purpose of
  • #1
jjlee2
3
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
  • #2
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.
 
  • #3
yes I've been using pcspim to run it, and that has a single-step function
 
  • #4
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.
 
  • #5
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
 
  • #6
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?
 

1. What is the purpose of converting numbers/letters to ASCII codes?

The purpose of converting numbers/letters to ASCII codes is to represent characters in a way that can be easily read and processed by computers. ASCII (American Standard Code for Information Interchange) is a standardized system that assigns numerical values to characters, making it easier for computers to store, transmit, and manipulate text.

2. How do you convert a number/letter to its corresponding ASCII code?

To convert a number/letter to its ASCII code, you can use an ASCII table which lists the numerical values for each character. Alternatively, you can use programming languages such as Python or JavaScript that have built-in functions for converting characters to ASCII codes.

3. Can ASCII codes represent all characters and symbols?

No, ASCII codes only represent a limited set of characters and symbols. Originally, ASCII only included 128 characters, which were primarily used in the English language. However, with the development of extended ASCII, some versions now include up to 256 characters, allowing for additional symbols and characters to be represented.

4. How is the ASCII code for a character determined?

The ASCII code for a character is determined by its position in the ASCII table. For example, the letter "A" has the ASCII code of 65, as it is the 65th character in the table. Similarly, the number "5" has the ASCII code of 53, as it is the 53rd character in the table.

5. Are there other encoding systems besides ASCII?

Yes, there are other encoding systems besides ASCII. Some examples include Unicode, which is a more comprehensive character set that supports multiple languages, and EBCDIC, which was used in older IBM computers. However, ASCII remains one of the most widely used encoding systems and is still the standard for basic character representation in computing.

Similar threads

  • Programming and Computer Science
Replies
4
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
3K
  • Programming and Computer Science
Replies
1
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Programming and Computer Science
Replies
5
Views
9K
  • Engineering and Comp Sci Homework Help
Replies
22
Views
4K
  • Programming and Computer Science
Replies
1
Views
7K
  • Programming and Computer Science
Replies
1
Views
8K
  • Programming and Computer Science
Replies
4
Views
7K
  • Programming and Computer Science
Replies
1
Views
5K
Back
Top