Convert Character to binary assembly language

前端 未结 2 1455
别那么骄傲
别那么骄傲 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.

提交回复
热议问题