这是我的算法,用于将数字除以2,直到商数达到0它停止,但我需要打印剩余部分,使其为二进制,我尝试在下面的代码中。但是当我运行它时,它不会打印任何东西。
a = userInput/2
if (a doesNotEqual 0)
{
while(quotient doesNotEqual 0)
{
quotient=a/2
a = quotient
}
}
!!!!!!
.section ".data"
prompt: .asciz "\nEnter a number: "
format: .asciz "%d"
prompt2: .asciz "\nPlease choose one of the following options: \n 1. Print the number in binary seperating each 4 bits with a space. \n"
prompt3: .asciz " 2. blahblahblahblahblahblahblahblahblahblahblahblah. \n 3. blahblahbalhblahblahbla. \n"
prompt4: .asciz " 4. blahblahblahblahblahblahblahb \n 5. Enter in a new number \n 6. Quit the program\n\n"
format2: .asciz "%d"
string: .asciz "\nYou entered option: %d\n"
string1: .asciz "%d"
define(a,l0)
define(quotient, l1)
define(remainder, l2)
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
.align 4
input: .word 0
input2: .word 0
.align 4
.section ".text"
.global main
main:
save %sp, -96, %sp
clr %a
clr %quotient
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
set prompt, %o0
call printf
nop
set format, %o0
set input, %o1
call scanf
nop
ld [%o1], %g1 ! g1 = userInput #
set prompt2, %o0 ! menu
call printf
nop
set prompt3, %o0
call printf
nop
set prompt4, %o0
call printf
nop
! menu
set format2, %o0
set input2, %o1
call scanf
nop
set input2, %o1
ld [%o1], %g2 ! g2 = option selected
set string, %o0
mov %g2, %o1
call printf
nop ! this is a check to see that g2 is indeed the option selected
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!problem somewhere below
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
mov %g1, %o0
call .div
mov 2, %o1
mov %o0, %a ! a = input #/2
cmp %a, 0 ! if (a doesNotEqual 0)
be done
test:
cmp %quotient, 0 ! while(quotient doesNotEqual 0)
be done1
nop
mov %a, %o0
call .div
mov 2, %o1
mov %o0, %quotient ! quotient = a/2
mov %a, %o0
call .rem
mov 2, %o1
mov %o0, %remainder
set string1, %o0
mov %remainder, %o1
call printf
nop
mov %quotient, %a ! a = quotient
ba test
nop
done:
done1:
ret
restore
答案 0 :(得分:0)
因为您在nl
和input
之间交错input2
,后者将不再对齐,因此总线错误。顺便说一句,我已经看到你多次做nl
这件事了,我相信我已经告诉过你这根本没有意义。 scanf
不使用第三个参数,因为格式字符串只有一个字段说明符,即使它有,也不会是换行符。
你可能还有其他问题,这只是我发现的第一个问题。学习使用调试器。