Converting Numbers/Letters to ASCII Codes

  • Thread starter Thread starter jjlee2
  • Start date Start date
Click For Summary

Discussion Overview

The discussion revolves around a program that converts numbers or letters to ASCII codes, specifically addressing an issue where the program only works correctly for up to four items before returning zeros. Participants are examining the code, debugging techniques, and the structure of the program.

Discussion Character

  • Technical explanation
  • Debugging
  • Conceptual clarification
  • Debate/contested

Main Points Raised

  • One participant describes a problem with their program, noting that it only processes up to four inputs correctly and then outputs zeros.
  • Another participant suggests using a debugger to single-step through the program to observe register and memory values.
  • A participant confirms they are using PCSpim, which has a single-step function for debugging.
  • It is noted that after entering four characters, the program begins storing subsequent characters in the next register, leading to confusion about how values are being stored.
  • A participant shares a modified version of the code with added comments for clarity, asking for corrections and further comments on the rest of the program.
  • Questions arise about the purpose of the variables used in the program, particularly regarding how characters and their ASCII codes are stored.
  • There is a request for clarification on the decrement operation in the loop and its necessity.

Areas of Agreement / Disagreement

Participants express varying levels of understanding regarding the program's behavior and the debugging process. There is no consensus on the exact cause of the issue or the best approach to resolve it, as multiple interpretations of the code and its execution are presented.

Contextual Notes

Participants highlight the importance of comments in assembly code for clarity, and there are unresolved questions about the handling of memory and register values, particularly after the fourth input.

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?
 

Similar threads

  • · Replies 4 ·
Replies
4
Views
2K
  • · 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
11K
Replies
1
Views
2K
  • · Replies 4 ·
Replies
4
Views
7K
  • · Replies 22 ·
Replies
22
Views
5K
  • · Replies 1 ·
Replies
1
Views
6K