-1

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
Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
ktos1234
  • 197
  • 1
  • 6
  • 22

2 Answers2

1

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 would also work instead of adc eax,0, and be more efficient on some CPUs.

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
msalihbindak
  • 592
  • 6
  • 21
  • `xor eax,eax` sets CF=0. Do that before `shl`. (And use `setc al` instead of `adc` for efficiency). [Also, the `LOOP` instruction is slow, don't use it.](https://stackoverflow.com/questions/35742570/why-is-the-loop-instruction-slow-couldnt-intel-have-implemented-it-efficiently) unless optimizing for code-size over speed. Also, you have `mov ebx, eax` inside the loop. Probably would have been better to test your asm before posting. – Peter Cordes Nov 21 '17 at 10:57
  • Also, you don't describe the calling-convention you're using. You appear to be using the Irvine32 convention, where the first arg goes in `eax`, and so does the return value. That's not at all standard or widely used. (Also, this is a 16-bit DOS question, although the algorithm is the same.) – Peter Cordes Nov 21 '17 at 11:01
  • BTW, welcome to Stack Overflow. If/when you debug this code, I'd be happy to reverse my downvote. It's nice and compact and has comments describing what it's doing, and is a decent answer except for the bugs. Notify me by including @peter in your comment. – Peter Cordes Nov 21 '17 at 11:27
  • Dear @PeterCordes thanks for reply, when I use SHL it sets CF , I do not care what is eax in this stuation. My number is in ebx not eax. Maybe I put the start label wrong line. I will edit and if you want take my code and fix it and share for us. – msalihbindak Nov 22 '17 at 22:32
  • Right, `shl` shifts a bit into CF, but then `xor eax,eax` clears CF before `adc eax,0` (which would be more efficiently written as `setc al`) – Peter Cordes Nov 22 '17 at 22:34
0

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.

Michael Petch
  • 46,082
  • 8
  • 107
  • 198
Frank Kotler
  • 3,079
  • 2
  • 14
  • 9