第54行有错误,不知道如何解决,
第54行在buf [$ s0]中有代码“sw $ t0,($ s2)#Store $ s2(第二个整数)。”
第54行出现错误:0x00400028处的运行时异常:存储地址未在字边界上对齐0x00000002
我也想知道我的代码是否正确?
我对此代码使用以下过程。
将缓冲区'buf'视为单词数组。您读取的第一个整数($ s0)是数组的索引(因为'buf'只有128个字节,第一个整数必须介于0和31之间)。将$ s2(您在步骤10中读取的第二个整数)存储到buf [$ s0]中。您需要执行的步骤包括:
将'buf'的起始地址加载到$ t0。
计算单词buf [$ s0]的偏移量(即$ s0中值的4倍)。
计算buf [$ s0]的地址。
将$ s2(第二个整数)存储在buf [$ s0]中。
打印出以下信息:
以小数表示的$ s0(第一个整数)(系统调用1)。
新行字符串(位置'换行符')。
将buf [$ s0]的地址加载到$ s1
以十六进制表示值$ s1(地址)(系统调用34)。
新行字符串(位置'换行符')。
以十六进制表示的值$ s2(第二个整数)。
.data #data segment
newline: .asciiz "\n" #ASCII for a new line. asciiz with z means 0 (not character '0') is placed after the last character.
.align 2 # The next variable (symbol) should starts at an address that is a multiple of 4.
name: .asciiz "CSE3666: Lab 0: YOUR NAME \n\n\n"
.align 2
msg1: .asciiz "\nThe first integer you just typed is\n"
.align 2
buf: .space 128 # Reserve space for a variable (array). Not
initialized.
.align 2
reserved: .space 20
.text # Code segment
.globl main # declare main to be global
main:
# load an address, which is a 32-bit value.
# Could be a symbol. See the example below. Still, the value is a 32-bit
value.
# la is a pseudo instruction.It is converted into two instructions.
#la $a0, 0xBF63C886
# Load a large constant with two instructions. Higher 16 bits will be 0
although the MSB of the constant is 1.
#lui $a0, 0xBF63
#ori $a0, 0xC886
# example of subtraction
#sub $a0, $a0, 1
# read user inputted integer
li $v0, 5
syscall
# store the integer into $s0
move $s0, $v0
# read user inputted integer
li $v0, 5
syscall
# store the integer into $s2
move $s2, $v0
#store $s0 to buf[$s0]
#address = base + index * 4
# la sll $s0 by 2
#la
#sll
#add
#sw
la $t0, buf #Load the starting address of ‘buf’ into $t0.
sll $s0, $s0, 2 #Calculate the offset of word buf[$s0] (i.e., 4 times the
value in $s0).
add $t0, $t0, $s0 #Calculate the address of buf[$s0].
sw $t0, ($s2) #Store $s2 (the second integer) in buf[$s0].
la $a0, msg1 #print message
li $v0, 4
syscall
lw $t0, buf
li $v0, 1 # system call, type 1, print an integer, *$a0
syscall # call the "OS"
la $a0, newline #new line
li $v0, 4
syscall
lw $t0, ($s1) #Value $s1 (the address) in hexadecimal
li $v0, 34
syscall
la $a0, newline #new line
li $v0, 4
syscall
lw $t0, ($s2) #Value $s2 (the second integer) in hexadecimal.
li $v0, 34
syscall
Exit: li $v0,10 # System call, type 10, standard exit
syscall