Converting decimal to binary in assembler

前端 未结 2 883
面向向阳花
面向向阳花 2021-01-29 10:27

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 shou

2条回答
  •  甜味超标
    2021-01-29 10:56

    Not entirely clear what you're trying to do. I guess "decimal to binary", but the prompt says "Enter binary value". I would take that to mean a string of "1"s and "0"s. I wouldn't ask 'em for a "decimal" value either - you'll get like "1.23", which you aren't equipped to handle. Just ask 'em for a number. Maybe "a number less than 65536", which is probably(?) what you want.

    Warning! Untested code ahead!

        Number proc
        mov cx,5 ; loop counter?
        xor bx,bx ; "result so far"?
    
    
        read:
        mov ah,0
        int 16h
    
    ; wanna give 'em the option to enter
    ; less than the full five digits?
        cmp al, 13 ; carriage return
        jz finis
    
        cmp al,'0'
        jb read
        cmp al, '9'
        ja read
        mov ah,0eh
        int 10h
    ; Assuming al still holds your character...
        sub al, '0' ; convert character to number
        mov ah, 0 ; make sure upper byte is clear
        imul bx, bx, 10 ; multiply "result so far" by 10
        ; jc overflow ;  ignore for now
        add bx, ax ; add in the new digit
        ; jc overflow ; ignore for now
    
        loop read
    finis:
    ; now our number is in bx
    ; it is conventional to return values in ax
        mov ax, bx
    
    overflow: ; I'm just going to ignore it
        ; spank the user?
        ; go right to exit?
        ret ; maybe endp generates this. two shouldn't hurt
        Number endp
    

    Now I guess you want to print the binary ("1"s and "0"s) representation of that number...

        Printbin proc
    ; what does "proc" do? Do you know?
        mov bx, ax
        mov cx, 16 ; 16 bits to do, right?
        mov ah, 2
     top:
        mov dl, '0'
        shl bx, 1 ; shift leftmost bit to carry flag
        adc dl, 0 ; bump the "0" up to "1", if set
        int 21h
        loop top
        ret
    
        endp ; ?
    

    Been a while since I've done DOS, so there may be serious errors in that, but it may give you some ideas.

提交回复
热议问题