Rndpbs
Rndpbs

Reputation: 47

Deci to bin converter (Pep/8 assembler and simulator)

I have an assignment to create a program that converts deci (-32,768 through 32,767) to bin. The output must show all 16 bits. For example, if the input is 120, output should be: 0000000001111000. I can't figure out how to output 0s and 1s in reverse. When I enter 120 I get: 0001111000000000. P.S: I'm using Pep/8 assembler and simulator (http://code.google.com/p/pep8-1/), which is available for Mac and PC. This is what I have so far:

;Pavel; Assignment 3
BR       main                ;Branch to MAIN
num:     .BLOCK  2           ;Input variable
flag:    .BLOCK  2           ;C flag
limit:   .BLOCK  2           ;Loop LIMIT
main:    LDA     0, i        ;Clear Accumulator
         DECI    num, d      ;Input 
loop:    LDA     limit, d    ;Load loop LIMIT
         CPA     16, i       ;Compare LIMIT to 16
         BREQ    exit        ;If LIMIT == 16, branch to EXIT. Done converting.
         LDA     num, d      ;Load NUM
         ASRA                ;Shift NUM to the right (division by 2)
         STA     num, d      ;Store NUM after division
if:      MOVFLGA             ;Load flags to Accumulator
         BRC     else        ;If C == 1, branch to ELSE
         DECO    0, i        ;Output 0
         LDA     limit, d    ;Load LIMIT
         ADDA    1, i        ;Add 1 to LIMIT
         STA     limit, d    ;Store LIMIT
         BR      loop        ;Branch to LOOP
else:    DECO    1, i        ;Output 1
         LDA     limit, d    ;Load LIMIT
         ADDA    1, i        ;Add 1 to LIMIT
         STA     limit, d    ;Store LIMIT
         BR      loop        ;Branch to LOOP
exit:    CHARO   ' ', i      ;Outputs space
         STOP
         .END

Upvotes: 1

Views: 1788

Answers (1)

Jens Björnhager
Jens Björnhager

Reputation: 5649

You are now shifting right and testing the bit that ends up in carry. What would happen if you shifted the other way?

Upvotes: 2

Related Questions