将字符转换为二进制汇编语言 [英] Convert Character to binary assembly language

查看:444
本文介绍了将字符转换为二进制汇编语言的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我使用dosbox和masm编译器。我想提示用户输入一个字符,并在下一行中以十六进制和二进制形式打印字符的ASCII码。重复此过程,直到用户输入回车符。下面的代码工作正常,以十六进制显示字符,直到回车,我怎样才能修改这段代码来显示charcter的二进制文件。

  .MODEL SMALL 
.STACK 100H

.DATA
PROMPT_1 DB 0DH,0AH,'输入字符:$'
PROMPT_2 DB 0DH, 0AH,'HEX格式给定数字的ASCII码是:$'

.CODE
MAIN PROC
MOV AX,@DATA;初始化DS
MOV DS,AX

@START:;跳转标签
LEA DX,PROMPT_1;加载并显示字符串PROMPT_1
MOV AH,9
INT 21H

MOV AH,1;读一个字符
INT 21H

MOV BL,AL;将AL移到BL

CMP BL,0DH;比较BL与CR
JE @END;如果BL = CR

LEA DX,PROMPT_2;则跳到标签@END;加载并显示字符串PROMPT_2
MOV AH,9
INT 21H

XOR DX,DX;清除DX
MOV CX,4;将4移至CX

@ LOOP_1:;循环标签
SHL BL,1;将BL左移1位
RCL DL,1;将DL向左旋转1个位置
;通过携带
LOOP @ LOOP_1;如果CX!= 0,跳转到标签@ LOOP_1

MOV CX,4;将4移至CX

@ LOOP_2:;循环标签
SHL BL,1;将BL向左移1位
RCL DH,1;将DH向左旋转1个位置
;通过携带
LOOP @ LOOP_2;如果CX!= 0,跳转到标签@ LOOP_2

MOV BX,DX;将DX移至BX
MOV CX,2;初始化循环计数器

@ LOOP_3:;循环标签
CMP CX,1;比较CX与1
JE @SECOND_DIGIT;如果CX = 1
,则跳转到标签@SECOND_DIGIT MOV DL,BL;将BL移至DL
JMP @NEXT;跳转到标签@NEXT

@SECOND_DIGIT:;跳转标签
MOV DL,BH;将BH移至DL

@NEXT:;跳转标签

MOV AH,2;设置输出功能

CMP DL,9;将DL与9
JBE @NUMERIC_DIGIT进行比较;如果DL <= 9
SUB DL,9则跳到标签@NUMERIC_DIGIT;将其转换为数字,即1,2,3,4,5,6
或DL,40H;将数字转换为字母,即A,B ... F
JMP @DISPLAY;跳转到标签@DISPLAY

@NUMERIC_DIGIT:;跳转标签
或DL,30H;将小数转换为ASCII码

@DISPLAY:;跳转标签
INT 21H;打印字符
LOOP @ LOOP_3;如果CX!= 0,跳转到标签@ LOOP_3

JMP @START;跳转到标签@START

@END:;跳转标签

MOV AH,4CH;返回控制到DOS
INT 21H
MAIN ENDP
END MAIN


解决方案

在寄存器中有值时,它以位(0/1编码为低/高电流电压)存储在CPU中,所以它实际上是以二进制格式化的! p>

您只需要输出八个字符'0' / '1'

在你有 AL 的字符的那一刻, ,输出二进制形式的代码可能如下所示:

  cx = 8; 8位输出
bin_loop:
rcl al,1;将最重要的位移入CF
setc bl;通过CF(80386指令)bl = 0或1
add bl,'0';将该0/1转换为'0'/'1'ASCII char
调用display_bl;必须保留al和cx
loop bin_loop

根据您对 cx for loop 你处于16b实模式。因此,如果你也不能使用80386指令( setc )(当定位8086/80186/80286 CPU时,如 emu8086 emulator),那么这可以通过其他方式实现(例如两条指令,而不是一条)。



shl / rcl 我相信你会发现一些东西,它非常相似。


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. Repeat this process untill the user types a carriage return. The following code work fine to display the charcters in hexadecimal until a carriage return how can i modify this code to display the binary of charcter as well.?

 .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 : $'

 .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 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

   JMP @START                 ; jump to label @START

 @END:                        ; jump label

 MOV AH, 4CH                  ; return control to DOS
 INT 21H
 MAIN ENDP
 END MAIN

解决方案

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.

这篇关于将字符转换为二进制汇编语言的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆