我想获得一个8位二进制数的反转和该反转的1的补码作为单个输出

时间:2019-07-03 11:33:11

标签: assembly emu8086

我已经编写了这段代码,但是我只得到了反向字符串作为输出。我也想要反向字符串的1的补码作为输出。 我知道该怎么办。当我编辑代码并将其准备好用于1的反向字符串的补码时,不再需要反向字符串!

测试用例:

  

输入:11010110

输出(需要):

  

反向字符串:01101011   1的补码:10010100

我的代码:

;REVERSING 8-BIT BINARY NO.

.MODEL
.STACK 100H
.DATA
    STR  DB 'Enter the binary number (max 8-bit) : $'
    STR2  DB 0DH,0AH,'REVERSED STRING : $'
    STR3 DB 'THE 1S COMPLEMENT OF IT: $'

.CODE
    MAIN PROC
    MOV AX, @DATA                  
    MOV DS, AX

    LEA DX, STR                  ; display STR   
    MOV AH, 9
    INT 21H


    XOR BL, BL                       ; CLEAR BL 
    MOV CX, 8                        ; loop for 8 times
    MOV AH, 1                        

    INPUT:                           
        INT 21H                      
        CMP AL, 0DH                  ; compare digit with carriage return (ENTER) 
        JZ END                       ; jump to END, if carriage return (JUMP IF ZERO)
        AND AL, 01H                  ; convert ascii to decimal code
        SHL BL, 1                    ; rotate BX to left by 1 bit
        OR BL, AL                    ; set the LSB of BX with input
        LOOP INPUT                   ; jump to INPUT

    END:                             
        MOV AL, BL                   ; copy BL into AL
        MOV CX, 8                    ; loop for 8 times

    LP:                              ; loop
        SHL AL, 1                    ; shift AL to left by 1 bit
        RCR BL, 1                    ; rotate BL right through carry
        LOOP LP                      ; jump to LP

    LEA DX, STR2                     ; load and display STR2   
    MOV AH, 9
    INT 21H

    MOV CX, 8                       
    MOV AH, 2                       ; output function


    OUTPUT:
      SHL BL, 1                     ; shift left BL by 1 bit
      JNC ZERO                      ; jump to label ZERO if CF=0
      MOV DL, 31H                   ; set DL=1. DL=0 for 1's compelement.
      JMP DISPLAY                   ; jump to DISPLAY

     ZERO:                        
       MOV DL, 30H                 ; set DL=0. DL=1 for 1's complement.

     DISPLAY:                     
       INT 21H                     ; display digit

     LOOP OUTPUT                   ; output


     MOV AH, 4CH                   
     INT 21H
   MAIN ENDP
 END MAIN

1 个答案:

答案 0 :(得分:2)

您可以在第52行上使用ROL代替SHL来将反转的值保留在BL寄存器中。现在您可以稍后重用它来输出1的补码值,如下所示:

    MOV CX, 8         ; For looping 8 times                      
    MOV AH, 2

_OUTPUT_INVERSE:
    ROL BL, 1         ; Rotate left by 1 bit
    JC  _I_ZERO       ; jump to label ZERO if CF=1
    MOV DL, 31H       ; set DL=1. DL=0 for 1's compelement.
    JMP _I_DISPLAY    ; jump to DISPLAY

_I_ZERO:                        
    MOV DL, 30H      ; set DL=0. DL=1 for 1's complement.

_I_DISPLAY:                     
    INT 21H

    LOOP _OUTPUT_INVERSE ; output the inverse

之所以起作用,是因为打印反向值后,BL寄存器包含 6B(即01101011)。因此,只需将剩余的值移动(或旋转)一次 在每个循环上将一反一地使用反值,该反值与以前一样打印 反向值。

完整代码:(使用您的样式)

;REVERSING 8-BIT BINARY NO.

.MODEL
.STACK 100H
.DATA
    STR  DB 'Enter the binary number (max 8-bit) : $'
    STR2 DB 0DH,0AH,'REVERSED STRING : $'
    STR3 DB 0DH,0AH,'THE 1S COMPLEMENT OF IT: $'

.CODE
    MAIN PROC
    MOV AX, @DATA                  
    MOV DS, AX

    LEA DX, STR         ; display STR   
    MOV AH, 9
    INT 21H


    XOR BL, BL          ; CLEAR BL 
    MOV CX, 8           ; loop for 8 times
    MOV AH, 1                        

    INPUT:                           
        INT 21H                      
        CMP AL, 0DH     ; compare digit with carriage return (ENTER) 
        JZ END          ; jump to END, if carriage return (JUMP IF ZERO)
        AND AL, 01H     ; convert ascii to decimal code
        SHL BL, 1       ; rotate BX to left by 1 bit
        OR BL, AL       ; set the LSB of BX with input
    LOOP INPUT          ; loop INPUT

    END:                             
        MOV AL, BL      ; copy BL into AL
        MOV CX, 8       ; loop for 8 times

    LP:                 ; loop
        SHL AL, 1       ; shift AL to left by 1 bit
        RCR BL, 1       ; rotate BL right through carry
    LOOP LP             ; jump to LP

    LEA DX, STR2        ; load and display STR2   
    MOV AH, 9
    INT 21H


    MOV CX, 8                       
    MOV AH, 2           ; output function


    OUTPUT_REVERSE:
      ROL BL, 1         ; rotate left BL by 1 bit
      JNC ZERO          ; jump to label ZERO if CF=0
      MOV DL, 31H       ; set DL=1. DL=0 for reversing the value.
      JMP DISPLAY       ; jump to DISPLAY

    ZERO:                        
      MOV DL, 30H       ; set DL=0. DL=1 for reversing the value.

    DISPLAY:                     
      INT 21H           ; display digit

    LOOP OUTPUT_REVERSE ; output reverse value

    LEA DX, STR3        ; load and display STR3   
    MOV AH, 9
    INT 21H


    MOV CX, 8                       
    MOV AH, 2           ; output function

    OUTPUT_INVERSE:
      ROL BL, 1         ; rotate left by 1 bit
      JC I_ZERO         ; jump to label ZERO if CF=1
      MOV DL, 31H       ; set DL=1. DL=0 for 1's compelement.
      JMP I_DISPLAY     ; jump to DISPLAY

    I_ZERO:                        
      MOV DL, 30H      ; set DL=0. DL=1 for 1's complement.

    I_DISPLAY:                     
      INT 21H

    LOOP OUTPUT_INVERSE ; output the inverse value 

    MOV AH, 4CH                   
    INT 21H
   MAIN ENDP
 END MAIN

输出:

Enter the binary number (max 8-bit) : 11010110
REVERSED STRING : 01101011
THE 1S COMPLEMENT OF IT: 10010100

顺便说一句,您不需要26和27行,因为循环仅运行8次。