Question

I want to basically remove my leading zeroes. When I print out a number for example 17 is 00000 0000 0000 0000 0000 0000 00001 0001 but to do remove those leading zeroes. Because in sparc machine that is what is printed out and I need to do this using some sort of loop or logic or shift function.

this is my psuedocode for printing the binary

      store input, %l1 ! store my decimal number in l1
      move 1,%l2       !move 1 into l2 register
      shift logical left l2,31,l2     !shift my mask 1 31 times to the left

     loop:
     and l2,l1,l3   ! do and logic between l1 and l2 and put this in l3
     compare l3,0    compare l3 zero
     bne print 1    !branch not equal to zero, to print 1

    if equal to 0
     print zero

     print 1:
      print a 1

     go: increment counter
      compare counter 32
      if counter less than 32 return to loop
      shift l2 to the right to continue comparison

so this is what is being done say my input is l1 is 17

00000 0000 0000 0000 0000 0000 00001 0001

10000 0000 0000 0000 0000 0000 00000 0000 and my mask 1 shift left 31 times

this pseucode print out my input decimal into binary. But how can I make it remove leading zeroes?

because in the sparc 17 input inside the machine is

0000 0000 0000 0000 0000 0000 0001 00001

Was it helpful?

Solution

You create the labels, like go and print 1 (more commonly done in all caps and without spaces, FYI). So, starting with bne you should always be printing 1, or falling through to see if it needs to print the 0:

    ! same initialization

    mov 0, l4         ! Initialize a flag to avoid printing

LOOP:

    and l2, l1, l3    ! do and logic between l1 and l2 and put this in l3

    cmp l3, 0         ! Is this a 0 digit?
    bne ALWAYS_PRINT  ! If it's not 0, then it must be 1 (was "bne print 1")

    cmp l4, 1         ! Should we be printing the 0?
    be PRINT_VALUE    ! Yes, we need to print the 0 because we have seen a 1

    ba INCREMENT      ! We should not be printing the 0, so check the next
                      !  digit (ba is "branch always")

ALWAYS_PRINT:         ! 

    mov 1, %l4        ! Note that we want to always print for the
                      ! rest of the loop

PRINT_VALUE:          ! Do whatever you're doing to print values

    print value in l3 ! Always print the value

INCREMENT:            ! Formerly your "go:" label

    ! same logic

    ! AFTER LOOP IS DONE LOGIC

    cmp l4, 0         ! If the flag was never set, then the value is 0
                      ! Alternatively, you could just compare the value to 0
                      !  and skip the loop entirely, only printing 0 (faster)

    bne DO_NOT_PRINT  ! If it was set (1), then do nothing
    print zero        ! If it wasn't set, then print the 0

DO_NOT_PRINT:

To walk through it a little, you need to continue to initialize your values and shift the bits to figure out what the current digit is for each iteration. Since you will need another flag, then you need to use another register that is initialized to an expected value (I chose 0, which commonly represents false).

  1. Get current digit into l3 (0 or 1)
  2. See if it is 0
  3. If it's not 0, then it must be 1. So go remember that we found a 1, for later, then print the value and increment/loop.
  4. If it's 0, then see if we have found a 1 before. If so, then print the value and increment/loop. If not, then increment/loop.

For actually printing, I have no idea what you are actually doing. However, you can avoid a second comparison by using the labels. For example, ALWAYS_PRINT will always be used when the value is 1, so you can just set the flag and immediately print 1, then jump to INCREMENT. If you did that, then PRINT_VALUE would only be used to print 0, which could then fall through to INCREMENT.

From a high level language's perspective, you want:

int l2 = // value...
bool seenOneAlready = false;

if (l2 != 0)
{
    // MSB first
    for (int i = 31; i > -1; --i)
    {
        int l3 = (l2 >> i) & 1;

        if (l3 == 1)
        {
            seenOneAlready = true;
            printf("1");
        }
        else if (seenOneAlready)
        {
            printf("0");
        }
    }
}
else
{
    printf("0");
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top