0

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
Selena Gomez
  • 69
  • 1
  • 7
  • This looks like SPARC to me? What platform are you on, have you tried using [gdb](http://www.cs.cmu.edu/~gilpin/tutorial/) to debug your code? You may also want to study the arguments to the [functions](https://www.gnu.org/software/libc/manual/html_node/Formatted-Input.html) you're calling here. – Jens Mar 22 '14 at 20:43

1 Answers1

0

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.

Jester
  • 56,577
  • 4
  • 81
  • 125
  • Im trying to convert the userInput # to binary. From the code above, it asks for userInput#, then it prints a menu, user selects a option, it prints the option, and it should convert the decimal to binary right after no matter what option you choose (getting the algorithm correct first). So for example the userInput = 9. Keep dividing by 2 until the quotient = 0 and the remainders from last to first is the binary number. 9/2=4 with remainder 1. 4/2=2 with remainder 0. 2/2=1 with remainder 0. 1/2=0 with remainder 1 So the binary for 9 is the remainders from last to first so it is 1001. – Selena Gomez Mar 23 '14 at 19:39