Convert Character to binary assembly language

前端 未结 2 1431
别那么骄傲
别那么骄傲 2021-01-16 18:13

Hi i m using dosbox and masm compilor. I want to prompts the user to enter a character, and prints the ASCII code of the character in hex and in binary on the next line. Rep

相关标签:
2条回答
  • 2021-01-16 18:26

    When you have the value in register, it is stored in CPU in bits (0/1 encoded as low/high current voltage), so it's actually "formatted" in binary!

    You just need to output eight characters '0'/'1' per bit, starting from most significant one.

    At the moment where you have the character in AL, the code to output the binary form may look like this:

        cx = 8   ; 8 bits to output
    bin_loop:
        rcl al,1 ; move most significant bit into CF
        setc bl  ; bl = 0 or 1 by CF (80386 instruction)
        add bl,'0' ; turn that 0/1 into '0'/'1' ASCII char
        call display_bl ; must preserve al and cx
        loop bin_loop
    

    Judging by your usage of cx for loop you are in 16b real mode. So if you also can't use 80386 instructions (setc) (when targetting 8086/80186/80286 CPU, like emu8086 emulator), then this can be achieved in other way (like two instructions for example, instead of one).

    From your usage of CF in those loops with shl/rcl I'm sure you will figure something out, it's very similar.

    0 讨论(0)
  • 2021-01-16 18:37

    Ok, Sarah, I made the changes to your code, now it displays binary, I added two variables, all the changes are pointed by little ◄■ arrows :

     .MODEL SMALL
     .STACK 100H
    
      .DATA
       PROMPT_1  DB  0DH,0AH,'Enter the character : $'
       PROMPT_2  DB  0DH,0AH,'The ASCII code of the given number in HEX       form     is : $'
       PROMPT_3  DB  0DH,0AH,'The ASCII code of the given number in BIN       form     is : $'
       MY_CHAR   DB  ?          ; ◄■ char entered by user.
       BINARY    DB  9 DUP('$') ; ◄■ zeroes and ones.
    
     .CODE
      MAIN PROC
     MOV AX, @DATA                ; initialize DS  
     MOV DS, AX
    
     @START:                      ; jump label 
       LEA DX, PROMPT_1           ; load and display the string PROMPT_1
       MOV AH, 9
       INT 21H
    
       MOV AH, 1                  ; read a character
       INT 21H                                      
    
       MOV MY_CHAR, AL ; ◄■ save char to use in binary conversion.
       MOV BL, AL                 ; move AL to BL
    
       CMP BL, 0DH                ; compare BL with CR
       JE @END                    ; jump to label @END if BL=CR
    
       LEA DX, PROMPT_2           ; load and display the string PROMPT_2
       MOV AH, 9
       INT 21H
    
       XOR DX, DX                 ; clear DX
       MOV CX, 4                  ; move 4 to CX
    
       @LOOP_1:                   ; loop label
         SHL BL, 1                ; shift BL towards left by 1 position
         RCL DL, 1                ; rotate DL towards left by 1 position
                                  ; through carry
       LOOP @LOOP_1               ; jump to label @LOOP_1 if CX!=0
    
       MOV CX, 4                  ; move 4 to CX
    
       @LOOP_2:                   ; loop label
         SHL BL, 1                ; shift BL towards left by 1 position
         RCL DH, 1                ; rotate DH towards left by 1 position
                                  ; through carry
       LOOP @LOOP_2               ; jump to label @LOOP_2 if CX!=0
    
       MOV BX, DX                 ; move DX to BX
       MOV CX, 2                  ; initialize loop counter
    
       @LOOP_3:                   ; loop label
         CMP CX, 1                ; compare CX wiht 1
         JE @SECOND_DIGIT         ; jump to label @SECOND_DIGIT if CX=1
         MOV DL, BL               ; move BL to DL
         JMP @NEXT                ; jump to label @NEXT
    
         @SECOND_DIGIT:           ; jump label
           MOV DL, BH             ; move BH to DL
    
         @NEXT:                   ; jump label
    
         MOV AH, 2                ; set output function
    
         CMP DL, 9                ; compare DL with 9
         JBE @NUMERIC_DIGIT       ; jump to label @NUMERIC_DIGIT if DL<=9
         SUB DL, 9                ; convert it to number i.e. 1,2,3,4,5,6
         OR DL, 40H               ; convert number to letter i.e. A,B...F
         JMP @DISPLAY             ; jump to label @DISPLAY
    
         @NUMERIC_DIGIT:          ; jump label
           OR DL, 30H             ; convert decimal to ascii code
    
         @DISPLAY:                ; jump label
           INT 21H                ; print the character
       LOOP @LOOP_3               ; jump to label @LOOP_3 if CX!=0
    
    
    ;▼ FROM CHAR TO BINARY ▼
         LEA SI, BINARY+7 ; ◄■ point to string in data segment.
         MOV CX, 8        ; ◄■ maximum number of binary digits.
       @BIN_CONVERSION:
         SHR MY_CHAR,1    ; ◄■ get rightmost bit.
         JC  @BIT1
         MOV [BYTE PTR SI], '0'
         JMP @BIN_SKIP
       @BIT1:
         MOV [BYTE PTR SI], '1'
       @BIN_SKIP:
         DEC SI
         LOOP @BIN_CONVERSION
    
         LEA DX, PROMPT_3 ; ◄■ display message.
         MOV AH, 9
         INT 21H         
         LEA DX, BINARY   ; ◄■ display binary.
         MOV AH, 9
         INT 21H         
    
       JMP @START                 ; jump to label @START
    
     @END:                        ; jump label
    
     MOV AH, 4CH                  ; return control to DOS
     INT 21H
     MAIN ENDP
     END MAIN
    
    0 讨论(0)
提交回复
热议问题