汇编 编程实现把键入的一个字符,用二进制形式(0/1)显示它的ASCII代码值。

编程实现把键入的一个字符,用二进制形式(0/1)显示它的ASCII代码值。

CODES SEGMENT
    ASSUME CS:CODES,DS:DATAS,SS:STACKS
START:
    MOV AX,DATAS
    MOV DS,AX

     MOV AH,01H   ; 键入一个字符
     INT 21H
   
     MOV CX, 8    

NEXT:ROL AL,1        ;右移一次
     JC OUT1          ;判断cf=0
     MOV DL,'0'    

     JMP DISP
OUT1:MOV DL,'1'
DISP:MOV AH, 02H
     INT 21H
     LOOP NEXT


    MOV AH,4CH
    INT 21H
CODES ENDS
    END START

  • 5
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我可以为您提供汇编语言的代码实现该功能。请查看以下代码: ```assembly section .data msg db 'Enter a character: ' ; prompt message hex db 'The ASCII code in hex is: 0x' ; message for hex value section .bss char resb 1 ; allocate one byte of memory for input character section .text global _start _start: ; print prompt message mov eax, 4 ; system call for write mov ebx, 1 ; file descriptor for stdout mov ecx, msg ; address of message to write mov edx, 17 ; number of bytes to write int 0x80 ; call kernel ; read one character from keyboard mov eax, 3 ; system call for read mov ebx, 0 ; file descriptor for stdin mov ecx, char ; address to store input character mov edx, 1 ; number of bytes to read int 0x80 ; call kernel ; convert input character to hexadecimal mov eax, 0 ; clear eax register mov al, [char] ; move input character to al register mov ebx, 16 ; move the base of hexadecimal to ebx xor edx, edx ; clear edx register div ebx ; divide al by ebx (16) add al, 0x30 ; convert remainder (0-9) to ASCII code cmp al, 0x3A ; check remainder (0-9) jna .hex ; jump to hex routine if remainder is less than 10 add al, 7 ; convert remainder (A-F) to ASCII code .hex: push eax ; push remainder to stack mov eax, 4 ; call write system call mov ebx, 1 ; write to stdout mov ecx, hex ; message for hex value mov edx, 27 ; number of bytes to write int 0x80 ; call kernel pop eax ; pop remainder from stack add al, 0x30 ; convert quotient (0-9) to ASCII code cmp al, 0x3A ; check quotient (0-9) jna .next ; jump to next character if quotient is less than 10 add al, 7 ; convert quotient (A-F) to ASCII code .next: mov ebx, 16 ; move the base of hexadecimal to ebx xor edx, edx ; clear edx register div ebx ; divide the quotient by ebx (16) push ax ; push remainder to stack cmp al, 0 ; check quotient jz .print ; jump to print routine if quotient is 0 add al, 0x30 ; convert quotient (0-9) to ASCII code cmp al, 0x3A ; check quotient (0-9) jna .next ; jump to next character if quotient is less than 10 add al, 7 ; convert quotient (A-F) to ASCII code jmp .next ; repeat until quotient is 0 .print: pop eax ; pop remainder (least significant bit) from stack add al, 0x30 ; convert remainder to ASCII code cmp al, 0x3A ; check remainder (0-9) jna .last ; jump to last character if remainder is less than 10 add al, 7 ; convert remainder (A-F) to ASCII code .last: mov ebx, 1 ; write to stdout mov ecx, eax ; result in al register mov edx, 1 ; number of bytes to write mov eax, 4 ; call write system call int 0x80 ; call kernel ; exit gracefully mov eax, 1 ; system call for exit xor ebx, ebx ; exit with code 0 int 0x80 ; call kernel ``` 您只需将上述代码保存为.asm文件,然后使用汇编器和链接器将其转换为可执行文件即可。请注意,此代码是针对x86架构编写的,如果您的计算机使用其他架构,则需要相应地更改代码

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值