Deci转bin转换器(Pep / 8汇编器和模拟器)

时间:2012-04-23 02:19:35

标签: assembly pep8-assembly

我有一项任务是创建一个将deci(-32,768到32,767)转换为bin的程序。输出必须显示全部16位。例如,如果输入为120,则输出应为:0000000001111000。我无法弄清楚如何反向输出0和1。当我输入120时,我得到:0001111000000000。 P.S:我正在使用Pep / 8汇编程序和模拟器(http://code.google.com/p/pep8-1/),它可用于Mac和PC。 这就是我到目前为止所做的:

;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

1 个答案:

答案 0 :(得分:2)

你现在正在向右移动并测试最终进位的位。如果换了另一条路会怎么样?