2014-12-06 64 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

相关问题