我正在尝试使用MARS在位图中绘制一个圆。我已经从Wikipedia转换了c公式,但是我得到的结果是错误的。我以为转换有误,但是我一生都无法弄清楚它是什么。
#Procedure: drawCircle:
#Draw a circle in the center of the input pixel (This will be implemented using the
#midpoint circle algorithm from https://en.wikipedia.org/wiki/Midpoint_circle_algorithm
#a0 = x0
#a1 = y0
#a2 = color
#a3 = radius
drawCircle:
#MAKE ROOM ON STACK
addi $sp, $sp, -20 #Make room on stack for 1 words
sw $ra, 0($sp) #Store $ra on element 0 of stack
sw $a0, 4($sp) #Store $a0 on element 1 of stack
sw $a1, 8($sp) #Store $a1 on element 2 of stack
sw $a2, 12($sp) #Store $a1 on element 3 of stack
sw $a3, 16($sp) #Store $a1 on element 4 of stack
#VARIABLES
move $t0, $a0 #x0
move $t1, $a1 #y0
move $t2, $a3 #radius
addi $t3, $t2, -1 #x
li $t4, 0 #y
li $t5, 1 #dx
li $t6, 1 #dy
li $t7, 0 #Err
#CALCULATE ERR (dx - (radius << 1))
sll $t8, $t2, 1 #Bitshift radius left 1
subu $t7, $t5, $t8 #Subtract dx - shifted radius
#While(x >= y)
circleLoop:
blt $t3, $t4, skipCircleLoop #If x < y, skip circleLoop
#Draw Dot (x0 + x, y0 + y)
addu $a0, $t0, $t3
addu $a1, $t1, $t4
lw $a2, 12($sp)
jal drawDot #Jump to drawDot
#Draw Dot (x0 + y, y0 + x)
addu $a0, $t0, $t4
addu $a1, $t1, $t3
lw $a2, 12($sp)
jal drawDot #Jump to drawDot
#Draw Dot (x0 - y, y0 + x)
subu $a0, $t0, $t4
addu $a1, $t1, $t3
lw $a2, 12($sp)
jal drawDot #Jump to drawDot
#Draw Dot (x0 - x, y0 + y)
subu $a0, $t0, $t3
addu $a1, $t1, $t4
lw $a2, 12($sp)
jal drawDot #Jump to drawDot
#Draw Dot (x0 - x, y0 - y)
subu $a0, $t0, $t3
subu $a1, $t1, $t4
lw $a2, 12($sp)
jal drawDot #Jump to drawDot
#Draw Dot (x0 - y, y0 - x)
subu $a0, $t0, $t4
subu $a1, $t1, $t3
lw $a2, 12($sp)
jal drawDot #Jump to drawDot
#Draw Dot (x0 + y, y0 - x)
addu $a0, $t0, $t4
subu $a1, $t1, $t3
lw $a2, 12($sp)
jal drawDot #Jump to drawDot
#Draw Dot (x0 + x, y0 - y)
addu $a0, $t0, $t3
subu $a1, $t1, $t4
lw $a2, 12($sp)
jal drawDot #Jump to drawDot
#If (err <= 0)
bgtz $t7, doElse
addi $t4, $t4, 1 #y++
addu $t7, $t7, $t6 #err += dy
addi $t6, $t6, 2 #dy += 2
j circleContinue #Skip else stmt
#Else If (err > 0)
doElse:
addi $t3, $t3, -1 #x--
addi $t5, $t5, 2 #dx += 2
sll $t8, $t2, 1 #Bitshift radius left 1
subu $t9, $t5, $t8 #Subtract dx - shifted radius
addu $t7, $t7, $t9 #err += $t9
circleContinue:
#LOOP
j circleLoop
#CONTINUE
skipCircleLoop:
#RESTORE $RA
lw $ra, 0($sp) #Restore $ra from stack
addi $sp, $sp, 20 #Readjust stack
我所称的任何程序都不会更改任何t寄存器。我得到的结果具有以下值:x = 10 y = 10颜色= 5半径= 10
答案 0 :(得分:0)
我想我已经解决了。问题不是转换,而是位图的大小。似乎当位图较小时,圆最终会环绕。