在循环问题中打印空间和新行

时间:2014-03-28 20:20:25

标签: loops assembly newline mips space

我编写的代码可以在第二个循环中继续使用新行和' totalmin'字符串未打印。有人可以告诉我如何解决这个问题?循环是正确的,因为打印值(没有空格)是正确的

我正在使用火星4.4并且还使用了4.3中的代码,但输出是相同的 代码:

#a1 is the minvalue base address for the minterms
#a2 holds value 1 
#a3 holds minvalue[i]
#t0 is the counter 
#t1 is the counter*4
#t2 holds the value of bit0[i]
#t3 holds the value of bit1[i]
#t4 holds the value of bit2[i]
#t5 holds the value of bit3[i]
#t6 holds the value of the compliment of bit0[i]....second use uses t6 as the product of bit0^bit1'
#t7 holds the value of bit2^bit3.... second use uses t7 as the product of (bit2^bit3)xor(bit1^bit0')
#t9 holds the min number
#s0 is the minvalue base address+(counter*4)
#s1 is the bit0 base address
#s2 is the bit1 base address
#s3 is the bit2 base address
#s4 is the bit3 base address
#s5 is the bit0 base address+(counter*4)
#s6 is the bit1 base address+(counter*4)
#s7 is the bit2 base address+(counter*4)
#t8 is the bit3 base address+(counter*4)
.data
bit0: .word 0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1 #array which holds values of bit zero
bit1: .word 0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1 #array which holds values of bit one
bit2: .word 0,0,0,0,1,1,1,1,0,0,0,0,1,1,1,1 #array which holds values of bit two
bit3: .word 0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1 #array which holds values of bit three

minvalue: .word 0   #array which holds the min term for each value 

space: .asciiz "   "                #creates a space when printing
newline: .asciiz "\n"               #creates a new line when printing
totalmin: .asciiz "the number of Minterms in F is:  "

.text
la $a1, minvalue        #base address for minterms
la $s1, bit0            #base address for bit0
la $s2, bit1            #base address for bit1
la $s3, bit2            #base address for bit2
la $s4, bit3            #base address for bit3  

li $t0, 0           #t0 is the counter which is set to 0
li $a2, 1           #a2 holds value 1
li $t9, 0           #min number is set to 0

loop:
sll $t1, $t0, 2         #multiplies the current value of t0 by 4 or t1=i*4
add $s0, $a1, $t1       #adds the minvalue base address by 4 each loop or s0=   minvbase+(i*4)
add $s5, $s1, $t1       #adds the bit0 base address by 4 each loop or s5= bit0base+(i*4)
add $s6, $s2, $t1       #adds the bit1 base address by 4 each loop or s6= bit1base+(i*4)
add $s7, $s3, $t1       #adds the bit2 base address by 4 each loop or s7= bit2base+(i*4)
add $t8, $s4, $t1       #adds the bit3 base address by 4 each loop or t8=bit3base+(i*4) 

lw $t2, 0($s5)          #t2 holds the value of bit0[i]
lw $t3, 0($s6)          #t3 holds the value of bit1[i]
lw $t4, 0($s7)          #t4 holds the value of bit2[i]
lw $t5, 0($t8)          #t5 holds the value of bit3[i]

not $t6, $t2            #t6 holds the value of the compliment of bit0[i]
and $t6, $t6, $t3       #t6 holds the value of bit1^bit0'
and $t7, $t4, $t5       #t7 holds the value of bit2^bit3
xor $t7, $t7, $t6       #t7 holds the value of (bit2^bit3)xor(bit1^bit0')

sw $zero, 0($s0)        #minterm[i] is intially set to zero
bne $t7, 0, minterm1        #if t7 is 1 then jump to minterm1

print:
lw $a3, 0($s0)          #a3 holds the value of minvalue[i]

li $v0, 1
move $a0, $t5
syscall

li $v0, 1
move $a0, $t4
syscall

li $v0, 1
move $a0, $t3
syscall

li $v0, 1
move $a0, $t2
syscall

li $v0, 4
la $a0, space
syscall

li $v0, 1
move $a0, $a3
syscall

li $v0, 4
la $a0, newline
syscall

addi $t0, $t0, 1        #increment counter or i++
bge $t0, 16, exit       #if t0 >= 16 jump to exit
j loop              #else jump to loop 


minterm1:
sw $a2, 0($s0)          #rewrites the minterm[i] from zero to 1
addi $t9, $t9, 1        #min number= min number+1
j print

exit:
li $v0, 4
la $a0, totalmin
syscall

li $v0, 1
move $a0, $t9
syscall

li $v0, 10
syscall

输出:

http://puu.sh/7N1hx.png

1 个答案:

答案 0 :(得分:0)

minvalue: .word 0   #array which holds the min term for each value 

您声明的数组只有一个32位字的空间,因此您的循环最终会覆盖位于minvalue之后的其他数据。

如果你想让minvalue保留16个单词,你应该使用类似的东西:

minvalue: .space 16*4