0
我正在为在MIPS汇编中输入的字符串实现一个基本的校验和算法作为在语言中工作的一般介绍,我可以使用一些错误检查。校验和汇编算法不加到正确值
这是我迄今为止,意见包括跟踪所发生的事情的每一步:
.data
str1:
.asciiz "This is a short string."
cs1:
.word 0x84a
str2:
.asciiz "This is a much longer string. In fact, it has two sentences in it, and some funny characters (~`)€."
cs2:
.word 0x230a
NonSuccessString:
.asciiz "You have more work to do."
Test1Success:
.asciiz "Test 1 was successful. "
Test2Success:
.asciiz "Test 2 was successful. "
.text
la $a0, str1 #Set $a0 to address of label str1:
jal checksum #Set $ra to address of next instruction, then jump to label checksum:
la $s0, cs1 #Set $s0 to address of label cs1:
lw $s1, 0($s0) #Set $s1 to contents of memory address $s0
beq $v0, $s1, Success1 # If $v0 and $sl are equal, jump to label Success1:
j NonSuccess # Jump to label NonSuccess:
Success1:
la $a0, Test1Success #Set $a0 to address of label Test1Success:
addi $v0, $zero, 4
syscall
la $a0, str2 #Set $a0 to address of label str2:
jal checksum #Set $ra to return address, then jump to label checksum:
la $s0, cs2 #Set $s0 to address of label cs2:
lw $s1, 0($s0) #Set $s1 to contents of memory address $s0
beq $v0, $s1, Success2 # If $v0 and $sl are equal, jump to label Success2:
j NonSuccess # Jump to label NonSuccess:
Success2:
la $a0, Test2Success #Set $a0 to address of label Test2Success:
addi $v0, $zero, 4
syscall
j Quit # Jump to label Quit:
NonSuccess:
la $a0, NonSuccessString #Set $a0 to address of label NonSuccessString:
addi $v0, $zero, 4
syscall
Quit:
addi $v0, $zero, 10
syscall
checksum:
addi $sp, $sp, -4 # adjust stack for 1 item
sw $s0, 0($sp) # save $s0
add $s0, $zero, $zero # i = 0
L1:
add $t1, $s0, $a0 # Locate the character at y[i]
lbu $t2, 0($t1) # Load the unsigned byte value of y[i]
beq $t2, $zero, L2 # exit loop if y[i] == 0
add $v0, $v0, $t2 # Add the byte to the total value
addi $s0, $s0, 1 # i = i + 1
j L1
L2:
jr $ra #jump back to $ra
在第一次测试的简单的字符串正确地计算和我得到的第一个“测试1是成功的。”消息,但在第二个校验和上,调试报告的总和为0x230e,比预期值多4,这意味着发生了一些不稳定的事情。我猜猜这些特殊字符可能会导致这个问题,但我不确定。有任何想法吗?
0x230a - 0x22de = 0x002c,这是','在ASCII,所以也许你没有把一个逗号?无论哪种方式,重置总和肯定是问题,看起来像现在一切正在工作。谢谢! –
我直接从您的问题中复制字符串。但只要它在你身边正常工作。 – Michael