2012-10-15 36 views
0

我想寫一個使用MARS(我的MIPS模擬器)的僞隨機數生成器來選擇一個隨機字符的字符串,把它拿出來的內存和到寄存器,並替換內存字符代碼帶星號'*'。MIPS隨機字加擾器

到目前爲止,它只是打亂了單詞的一部分,它的駕駛我瘋了。我無法找到此代碼無法工作的內容。我甚至都不需要直接回答,只是提示/提示會這麼有用。

下面是代碼:

#this loop extracts a char at random from a string in memory, stores it in a register, and replaces the char in the string with an asterisk '*' 

.data 

.align 2 
string0: .ascii "Tyler\n" 

.align 2 
endString: .asciiz "Loop completed!\n" 
.align 2 
scrambleString: .asciiz 


.text 

#counter 
li $t0, 5 

#pointer to string0 
la $s0, string0 




loop2: 

#is counter = 0? go to loop3 if so 
beq $t0, $0, loop3 

#seed & prepare randomized number generator 
li $v0, 30 
syscall 

li $v0, 40 #sets seed 
syscall 

#generates random number in $a0, with the coUnter $t0 being the upper bound 
addi $a1, $t0, 1 
li $v0, 42 
syscall 


#add STRING POINTER by random number in $a0, store this new address in $t1 
#addi $a0, $a0, 1 
add $t1, $s0, $a0 
#srlv $t1, $s0, $a0 

#isolates that bytesized char, puts it into $t2 
lbu $t2, ($t1) 
#beq $t2, 0x5c, loop2 

#replaces char in original string with "*" 
li $t3, 0x2a 
sb $t3, ($t1) 

beq $t1, $t3, loop2 
#decrement counter 
addi $t0, $t0, -1 

#loop return 
j loop2 


loop3: 
la $a0, string0 
li $v0, 4 
syscall 

li $v0, 10 
syscall 
+1

我完全不知道MIPS asm,也不知道系統調用使用的調用約定,但是您確定在系統調用期間保存$ t0和其他臨時寄存器嗎? – Serge

回答

0
  1. 你重新上 你的循環的每個迭代(環2 :)隨機數種子。
  2. 系統調用40和42各取2個參數,應該在$a0$a1中。見here
  3. 臨時寄存器$t0,...,$t9每個 syscall得到改變。您應該使用被叫方保存的寄存器 $s0,...$s8代替。