2014-12-06 4 views
-1

어셈블러에서 첫 번째 프로그램에 대한 도움이 필요합니다. 사용자가 입력 한 값을 10 진수에서 2 진수로 변환해야합니다. 어떻게 값을 소수점으로 표시 할 수 있는지, 그리고 다음에해야 할 일은 잘 모릅니다. 누구든지 다음 단계에 대해 단계별로 안내 할 수 있습니까?어셈블러에서 10 진수를 2 진수로 변환

.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

뭘 하려는지 명확하게 밝히지 않았습니다. 나는 "10 진수에서 2 진수"로 추측하지만 프롬프트에 "2 진 값 입력"이라고 표시됩니다. 나는 "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 

는 지금은

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를 한 적이되어 있기 때문에 ... 당신은 바이너리 ("1"과 "0") 그 숫자의 표현을 인쇄 할 것 같아요 , 그래서 거기에 심각한 오류가있을 수 있지만 그것은 당신에게 몇 가지 아이디어를 줄 수 있습니다.

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' 전에 해봐. (효율성을 높이기 위해'adc' 대신'setc al'을 사용하십시오). [또한, LOOP' 명령은 느리므로 사용하지 마십시오.] (https://stackoverflow.com/questions/35742570/why-is-the-loop-instruction-slow-couldnt-intel-have-implemented -it-efficient) 코드 크기를 오버 스피드로 최적화하지 않는 한. 또한 루프 안에'mov ebx, eax'가 있습니다. 게시하기 전에 asm을 테스트하는 것이 더 좋았을 것입니다. –

+0

또한 사용중인 호출 규칙에 대해서는 설명하지 않았습니다. 당신은 첫 번째 arg가'eax'에 들어가는 Irvine32 규칙을 사용하는 것처럼 보입니다. 반환 값 역시 마찬가지입니다. 그것은 전혀 표준 적이거나 널리 사용되지 않습니다. (알고리즘은 동일하지만 16 비트 DOS 질문입니다.) –

+0

BTW, Stack Overflow에 오신 것을 환영합니다. /이 코드를 디버깅 할 경우, 내 downvote 반대로 행복 할 거라고. 멋지면서도 간결하며 그 일을 설명하는 주석이 있으며 버그를 제외하고는 괜찮은 대답입니다. 귀하의 의견에 @peter를 포함하여 알려주십시오. –

관련 문제