MIPS Assembly 错误的数字

huangapple go评论77阅读模式
英文:

MIPS Assembly wrong digit

问题

The reason you need to subtract 48 from each digit in your code is to convert the ASCII character representation of numbers to their actual numeric values. In ASCII, the character '0' has a decimal value of 48, '1' has a value of 49, '2' has a value of 50, and so on.

So, when you read a character from your string (which is in ASCII encoding), you need to subtract 48 from it to get the actual numeric value. For example, when you read '1' from the string, its ASCII value is 49, but you want it to be treated as the number 1, so you subtract 48, resulting in 1.

Here's a breakdown of the process:

  • Read '1' (ASCII 49) from the string.
  • Subtract 48: 49 - 48 = 1.

By performing this subtraction for each digit in your string, you're correctly converting the ASCII characters to their corresponding numeric values, which allows you to calculate the sum of the numbers correctly.

英文:
la $a0, number
li $t3, 0     #Iterator = 0
li $v1, 0     #Sum = 0

while:
add $t1, $a0, $t3 	#t1 = &A[i]
		
lb $t1, 0($t1)		# A[i]
	
beq $t1, $zero, endwhile

add $v1, $v1, $t1	    # Sum

addi $t3, $t3, 1    	# Iterator + 1

subi $v1, $v1, 48	    # ???? Every digit is added with 48, so i have to subtract but why ???

j while
endwhile:

li $v0, 1       #Print the sum
move $a0, $v1 
syscall 

Can someone pls help me. Why do i have to subtract every digit with 48 to get the right result?
I dont know why its keep adding every time 48 to the digit from my string.
As example i have the String: "1234" if i dont subtract every digit with 48 the result is 202. With subtraction of 48 for every digit the right result is 10.

答案1

得分: 0

看起来,你想要计算一个数字字符串的各个数字之和(例如"1234")。在MIPS内存中,"1234"字符串存储为以下5个字节:

49、50、51、52、0

第0个字节 - 十进制49(代表1的ASCII码)
第1个字节 - 十进制50(代表2的ASCII码)...等等
第n个字节 - 十进制0(代表空字符的ASCII码,表示MIPS字符串的结束)

考虑你代码中的这一行 -

lb $t1, 0($t1)      # A[i]

这会加载A[i]处的字节值。在第一次迭代中,它会存储49(代表'1'的ASCII码),而不是'1'本身。

现在,要从ASCII值中获取数字本身,我们需要从中减去ASCII值'0'。

也就是说,数字d的ASCII码值 - '0'的ASCII码值 = 数字d的数值(考虑一下)。
现在'0'的ASCII码是48。这就是你在减去的值。

如果没有这个减法操作,你最终会将1、2、3、4的ASCII值相加,得到202。

英文:

Looks like, given a number as a string (say "1234"), you are trying to get the sum of all the digits. "1234" string is stored as following 5 bytes in MIPS memory -
49, 50, 51, 52, 0

0th byte - decimal 49 (ASCII of 1)<br>
1st byte - decimal 50 (ASCII of 2) .. etc.<br>
nth byte - decimal 0 (ASCII of null character - indicates the end of a string to MIPS)

Consider this line from your code -

lb $t1, 0($t1)      # A[i]

This loads the byte value at A[i]. In the first iteration, this stores 49 (ASCII of '1'), not '1' itself.

Now to get the digit itself from it's ASCII value, we need to subtract ASCII of '0' from it.

that is, ASCII of digit d - ASCII of 0 = numeric value of d (think about it).<br>
Now ASCII of 0 is 48. That is what you are subtracting.

In its absence, you end up summing the ASCII values of 1, 2, 3, 4, which is 49+50+51+52 = 202

huangapple
  • 本文由 发表于 2020年1月4日 00:30:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/59582044.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定