所以我开始在 Windows 机器上使用 NASM 学习 16 位汇编。如果有我创建的这个小程序,它要求用户输入,然后确定输入是否在某个范围内(0 到 9)。如果是,它继续查看该值是否可以被三整除,如果不是,它应该循环并询问用户另一个值。这是我的代码:
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大于它应该跳转到 notEqual 部分,否则跳转到结束部分。
就像现在一样,我总是显示 finalMsg,只有当值完全可以被 3 整除时才应该显示。
任何人都有一些建议。谢谢。J.P。