2014-12-06 61 views
-1

我需要編譯器中第一個程序的幫助。 我必須將用戶輸入的值從十進制轉換爲二進制。 我不知道如何顯示值作爲小數,然後我應該做什麼。 任何人都可以指導我一步一步做下一步。將彙編器中的十進制轉換爲二進制文件

.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 

回答

0

不完全清楚你想要做什麼。我猜「十進制到二進制」,但提示說「輸入二進制值」。我會認爲這是一串「1」和「0」。我不會問他們「小數」值 - 你會得到像「1.23」,你沒有裝備處理。只要問他們一個號碼。也許「少於65536」,這可能是(?)你想要的。

警告!未經測試的代碼!

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 

現在,我想你要打印的二進制文件(「1」和「0」),這個數字的表示......

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 ; ? 

已經有一段時間,因爲我已經做了DOS ,所以可能會有嚴重的錯誤,但它可能會給你一些想法。

0

我認爲這對你會好的。

; 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更有效率。

+0

'xor eax,eax'設置CF = 0。在'shl'之前這樣做。 (並且使用'setc al'代替'adc'來提高效率)。 [另外,'LOOP'指令很慢,不要使用它。](https://stackoverflow.com/questions/35742570/why-is-the-loop-instruction-slow-couldnt-intel-have-implemented除非針對代碼大小超速進行優化。另外,你在循環中有'mov ebx,eax'。在發佈之前測試你的asm可能會更好。 – 2017-11-21 10:57:06

+0

另外,您不會描述您正在使用的調用約定。您似乎使用Irvine32慣例,其中第一個參數在'eax'中,而返回值也是如此。這完全沒有標準或廣泛使用。 (另外,這是一個16位DOS問題,雖然算法是相同的。) – 2017-11-21 11:01:16

+0

順便說一句,歡迎來到Stack Overflow。如果/當您調試此代碼時,我很樂意反轉我的失敗。它很好,很緊湊,並有評論描述它在做什麼,除了這些錯誤之外,它是一個體面的答案。通過在您的評論中添加@peter來通知我。 – 2017-11-21 11:27:20

相關問題