2013-09-24 2 views
1

그래서 Windows 컴퓨터에서 NASM을 사용하여 16 비트 어셈블리를 학습하기 시작했습니다. 내가 만든이 작은 프로그램에서 사용자에게 입력을 요청한 다음 입력이 특정 범위 (0-9) 내에 있는지 확인합니다. 그럴 경우 값이 3으로 나눌 수 있는지 확인하고 그렇지 않은 경우 사용자에게 다른 값을 요청합니다. 내 코드는 다음과 같습니다.16 비트 어셈블리 프로그램

org 0x100 
    bits 16 
;jump over data declarations 
    jmp main 

input: 
    db 6 
    db 0 
user: times 6 db ' ' 

cr_lf: db 0dh, 0ah, '$' 

message: db 'Please enter the number you select between 0 and 9:','$' 
errormsg: db '***', 0ah, 0dh, '$' 
finalMsg: db 'Number is divisible by 3!', 0ah, 0dh, '$' 
finalErrorMsg: db 'Number is not divisible by 3!', 0ah, 0dh, '$' 

outputbuffer: db '  ', '$' 

;clear screen and change colours 
clear_screen: 
    mov ax, 0600h 
    mov bh, 17h ;white on blue 
    mov cx, 00 
    mov dx, 184Fh 
    int 10h 
    nop 
    ret 

move_cursor: 
    mov ah, 02 
    mov bh, 00 
    mov dx, 0a00h 
    int 10h 
    ret 

;get user input 
get_chars: 
    mov ah, 01 
    int 21h 
    ret 

;display string 
display_string: 
    mov ah, 09 
    int 21h 
    ret 

errstar: 
    mov dx, errormsg  
    call display_string 
    int 21h 
    jmp loop1 

nextphase: 
    cmp al, 30h  ;compare input with '0' i.e. 30h 
    jl errstar  ;if input is less than 0, display error message 
    ;else 
    call ThirdPhase  ;input is clearly within range 

ThirdPhase: 
    xor dx, dx  ;set dx to 0 for the divide operation 
    ;at this point al has the value inputted by the user 
    mov bl, 3 ;give bl the value 
    div bl  ;divide al by bl, remainder stored in dx, whole stored in ax 
    cmp dx, 0 ;compare remainder to 0 
    jg notEqual ;jump to not divisible by three as remainder is greater than 0 
    je end 

notEqual: 
    mov dx, finalErrorMsg 
    call display_string 
    int 20h 

end: 
    mov dx, finalMsg 
    call display_string 
    int 20h 

;main section 
main: 
    call clear_screen ;clear the screen 
    call move_cursor ;set cursor 

loop1: 
    mov dx, message  ;mov display prompt into dx 
    call display_string ;display message 
    call get_chars  ;read in character 
    ;at this point a character value is inputted by the user 
    cmp al, 39h  ;compare with '9' i.e. 39h 
    jle nextphase  ;if value is less than or equal to 9, move onto next phase 
    jg errstar  ;else call error and loop 

어쨌든 값 범위 검사가 잘 작동하고 반복도 잘 작동합니다. 내가 가진 문제는 ThirdPhase 세 섹션에서 나눌 수 있습니다. 제 생각에는 dx에 0 값이 포함되어 있는지 확인해야합니다. 그런 다음 값 3을 bl로 이동하십시오. 이제 al은 사용자 입력을 포함하고 bl은 값 3을 포함하고 dx는 0입니다. div 블럭 부분에서 al은 3의 값인 bl로 나뉩니다. 나머지는 dx에 저장되고 0과 비교하면 위도가 커지면 notEqual 부분으로 점프해야하며 그렇지 않으면 끝 부분으로 건너 뜁니다. 그것은 지금처럼

, 난 항상 값이 몇 가지 제안을 가지고 3.

누군가에 의해 완전히 나눌 경우에만 표시하도록되어있는의 finalMsg 표시 얻을. 감사합니다. . JP.

+0

'finalErrorMsg'를 출력 한 후에 프로그램을 종료하지 마십시오. 그래서, 그 후에'finalMsg'를 출력합니다. 빠른 수정 :'end' 레이블에 도달하기 바로 전에'loop1'로 점프합니다. – Kamiccolo

답변

3

바이트로 나누는 div bl을하고 있습니다. 따라서 코드는 al이고 나머지는 ah이고 axdx이 아니라 코드가 있다고 가정합니다. 배당금이 al의 단일 바이트이기 때문에 div 전에 ah을 삭제해야합니다.

+0

좋아, 그래서 비교 문을 변경했습니다 : cmp 아, 0 그러나 지금은 항상 세 부분으로 나눌 수없는 점프. 이전과는 정반대입니다. –

+1

@Slasher_X 전에 손에 '아'를 지우셨습니까? – lurker

+0

아하! 아를 지우는 일은 미리 놀라운 일을했습니다. –