Converting decimal to binary in assembler

前端 未结 2 855
面向向阳花
面向向阳花 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 11:08

    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.

提交回复
热议问题