在汇编器中将十进制转换为二进制 [英] Converting decimal to binary in assembler

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

问题描述

我在汇编程序中的第一个程序需要帮助.我必须将用户输入的值从十进制转换为二进制.我不知道如何将值显示为小数,下一步该怎么做.谁能一步一步指导我下一步该怎么做.

I need help with my first program in assembler. I have to convert values entered by user from decimal to binary. I have no idea how can I show values as a decimal, and what should I do next. could anyone instruct me step by step what do next.

    .model small
    .stack 100h`

    .data
            txt1 db "Enter binary value:" ,10,13, "$"
            txt2 db "BIN: " ,10,13, "$"


    .code

        main proc
        mov ax, @data
        mov ds, ax
        ;clear screen
        mov ah,0fh
        int 10h
        mov ah,0
        int 10h
        ;show first text
        mov ah, 9
        mov dx, offset txt1
        int 21h
        call Number


        main endp


        Number proc
        mov cx,5
        xor bx,bx

        read:
        mov ah,0
        int 16h
        cmp al,'0'
        jb read
        cmp al, '9'
        ja read
        mov ah,0eh
        int 10h
        loop read
        Number endp

        mov ax, 4c00h
        int 21h

        end main

推荐答案

我认为这对您来说还可以.

I think it will be ok for you.

; Read an integer from the screen and display the int in binary format
; and continue until number is negative.
again:            ; For loop
    call read_int ; take the integer from screen
    cmp eax,0     ; look if number is not negative
        JL end:       ; if less than zero program ends.
    mov ecx,32    ; for loop we set ecx to 32 ; ATTENTION we not specified type. So compiler will get error.

    mov ebx,eax   ; we will lost our number in eax, so I take it to ebx
START:
    xor eax,eax   ; eax = 0
    SHL ebx,1     ; shift the top bit out of EBX into CF
    ADC eax,0     ; EAX  = EAX + CF + 0 ADD CARRY FLAG, so eax is zero we add zero. The new eax will exact value of Carry Flag which is out bit.
    call print_int ; Then we print the CF which we took the eax.
LOOP start:   ; Loop looks ecx if not 0 it goes start.  
call print_nl ; For next number we print a new line
JMP again:    ; For take new number

    END:      ; End of the program.

setc al 也可以代替 adc eax,0 ,并且在某些CPU上效率更高.

setc al would also work instead of adc eax,0, and be more efficient on some CPUs.

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

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