Question

This is my algorithm to keep dividing the number by 2 until the quotient reaches 0 it stops, but i need to print the remainders for it to be in binary and i attempted in my code below. But when i run it , it doesn't print anything.

 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
Was it helpful?

Solution

Because you interleave the nl between input and input2, the latter will no longer be aligned, hence the bus error. By the way, I have seen you do this nl thing multiple times, and I believe I have told you it makes no sense at all. scanf doesn't use the third parameter because the format string only has one field specifier, and even if it did, it wouldn't be a newline.

You might have other problems too, this is just the first one I spotted. Learn to use a debugger.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top