我在学校做一个准备,显示一个骰子可能出现的三个结果的程序,为此使用了一个数组,但是我只能使其遍历该数组并打印所有值,而不是在所有三个结果均未选中时选择一个值有不同的概率。
.data
dragon: .asciiz "Dragon"
orc: .asciiz "Orc"
sword: .asciiz "sword"
dice: .word dragon, dragon, dragon, dragon, dragon, orc, orc, orc,
sword, sword, sword, sword
iterator: .word 0
size: .word 11
.text
main:
la $t0, dice
lw $t1, iterator
lw $t2, size
beginLoop: bgt $t1, $t2, exitLoop
sll $t3, $t1, 2
addu $t3, $t3, $t0
addi $t1, $t1, 1
li $v0, 4
lw $a0, 0($t3)
syscall
j beginLoop
exitLoop: li $v0, 10
syscall
您甚至不必将随机的32位整数映射到您的范围;如果您使用系统调用42而不是41,那么MARS会为您执行此操作。系统调用将RNG的ID#作为输入,但是似乎您不需要初始化它,只需使用0
.data
dragon: .asciiz "Dragon\n"
orc: .asciiz "Orc\n"
sword: .asciiz "Sword\n"
# this can't be line-wrapped, unless you use another .word directive
dice: .word dragon, dragon, dragon, dragon, dragon, orc, orc, orc, sword, sword, sword, sword
#.equ die_size, (. - dice)/4
#die_size = (. - dice)/4
.eqv die_size, 12 # 11 + 1
# MARS built-in assembler is crap and can't calculate the size for us at assemble time.
.text
main:
li $v0, 42 # Service 42, random int range
li $a0, 0 # Select random generator 0
li $a1, die_size # upper bound of range (non-inclusive)
syscall # invoke the system call, returns in $a0
# $a0 is in the range [0..12), i.e. 0..11
sll $a0, $a0, 2 # scale by size of word, $a0 is a byte index into pointer array
lw $a0, dice($a0)
li $v0, 4 # print string
syscall
li $v0, 17
li $a0, 0
syscall # exit(0) because MARS doesn't support returning from main!?!
# jr $ra
我将我的MARS配置为将数据段放置在地址空间的低16kiB中,因此我可以使用dice($reg)
对该数组进行索引。如果不这样做,则可以使用addu
进行地址数学运算。MARS的内置汇编器确实很烂,似乎迫使您将数组的大小硬编码为文字数字。在像GAS这样的真实汇编程序中,您将在组装时使用
.equ die_size, (. - dice)/4 + 1
根据指针数组中的元素数来计算它。