模数余数显示
Display of modulus remainder
美好的一天,这里是 nasm 的新手。
这是 win32(org 0x100) 中的 运行。
想要在给出输入(1-9)后在 mess4 "the remainder is:" 旁边显示模数余数,如果是 7 或 8 或 9,则给出模数的余数。
另一个问题是仍然习惯语法,所以我的 "cmp" 从输入到 7(也是 8 和 9,因为 7 可以分为 8,9,余数为 1,2)捕获输入值 1 和 2,因为这是与 8 和 9 除以 7 后的余数相同,余数为 1 和 2,因此读数错误。
任何帮助,将不胜感激。谢谢!
尝试过更改和迁移注册商,但无济于事。
更新:我能够在屏幕上显示余数,唯一的问题是在除以 7 时获得适当的调用,以便它仅在给出值 7、8、9 时调用 'isdiv'。
bits 16
org 0x100 ; start offset at memory position 100
jmp main ; jump to main program
;
; Data definitions
;
mess1: db 'Input any number (1 - 9)', 0dh,0ah,'$'
mess2: db 'The number is divisible by 7',0dh,0ah,'$'
mess3: db 'The number is not a divisible by 7',0dh,0ah,'$'
mess4: db 'The remainder is: ',0ah, 0dh,'$' ;Not sure
errmess: db '**',0dh,0ah,'$'
crlf: db 0dh,0ah, '$'
;
; Display a string on the screen
; DX contains the address of the string
;
display:
mov ah,09
int 21h
ret
;
;Display the remainder
remainder:
mov dx,mess4
call display
ret
; Set the cursor position
;
cursor:
mov ah,02
mov bh,0 ; screen number mov
mov dh,05h ; row
mov dl,0 ; column
int 10h
ret
;
; Display a user prompt
;
prompt:
mov dx,mess1
call display
ret
;
; Read one character from the keyboard
;
input:
mov ah,01
int 21h
ret
;
; Clear screen and change screen colour
;
screen:
mov ah,06 ; scroll up screen
mov al,0 ; lines to scroll where 0 clear entire screen
mov cx,0 ; starting row:column
mov dl,80 ; ending row;column
mov dh,80
mov bh,00011110b ; colour: yellow on blue
int 10h
ret
;
; Carriage returnm and line feed
;
newline:
mov dx,crlf
call display
ret
;
; Main program
;
main:
call screen
call cursor
next:
call prompt
call input
cmp al,'1' ; character < 1?
jl error ; yes, error message
cmp al,'9' ; character > 9?
jg error ; yes, error message
sub al,30h ; convert from ASCII to numeric
xor ah,ah ; clear AH
mov bl,7
idiv bl ; divide by 7
mov ch,ah
;cmp ah,0 ; remainder = n0?
je isdiv ; yes: divisible by 7
call newline
mov dx,mess3 ; not divisible by 7
call display
jmp fin
isdiv:
call newline
mov dx,mess2
call display ; divisible by 7
call remainder
add ch,30h
mov dl,ch
mov ah,2h
int 21h
fin:
;
int 20h ; terminate program
; Display error message. Number out of range
;
error:
mov dx,errmess
call display
jmp next
添加了 mov cl,al ;复制初始输入供以后在 cmp cl,7 中使用,并使用 jge isdiv 显示余数。
这解决了我的问题。
idiv bl ; divide by 7
mov ch,ah
;cmp ah,0 ; remainder = n0?
je isdiv ; yes: divisible by 7
idiv
指令没有定义任何标志。如果不显式地将 AH
与零进行比较,条件跳转将无法正常执行。
当代码发现输入的数字不能被7整除时,它不应该跳到最后(jmp fin
),而是跳到一个你可以决定是否要求显示余数的地方。
您只想查看输入“7”、“8”和“9”的余数。
您可以轻松地过滤掉这些,因为对于较小的输入“1”到“6”,商将是 1 而不是 0!
sub al, 30h ; convert from ASCII to numeric
cbw
mov bl, 7
div bl
mov cx, ax ; SAVE for parts 1 and 2 of the task
call newline
test ch, ch ; Remainder saved earlier
jz isdiv ; Divisible by 7
mov dx, mess3 ; Not divisible by 7
call display
jmp part2
isdiv:
mov dx, mess2
call display
part2:
cmp cl, 1 ; Quotient saved earlier
jne fin ; Input was "1", "2", ... , "6"
mov dx, mess4
call display
mov dl, ch ; Remainder saved earlier
add dl, 30h
mov ah, 02h
int 21h
call newline
fin:
int 20h
mess4: db 'The remainder is: ',0ah, 0dh,'$' ;Not sure
冒号 : 表明您希望余数显示在 后面 文本。最好放下回车 return 然后换行。您可以在之后调用单独的 newline。
mess4: db 'The remainder is: $'
screen:
mov ah,06 ; scroll up screen
mov al,0 ; lines to scroll where 0 clear entire screen
mov cx,0 ; starting row:column
mov dl,80 ; ending row;column
mov dh,80
mov bh,00011110b ; colour: yellow on blue
int 10h
ret
80x25 文本屏幕以 DL=79
列和 DH=24
行结束。
美好的一天,这里是 nasm 的新手。 这是 win32(org 0x100) 中的 运行。 想要在给出输入(1-9)后在 mess4 "the remainder is:" 旁边显示模数余数,如果是 7 或 8 或 9,则给出模数的余数。 另一个问题是仍然习惯语法,所以我的 "cmp" 从输入到 7(也是 8 和 9,因为 7 可以分为 8,9,余数为 1,2)捕获输入值 1 和 2,因为这是与 8 和 9 除以 7 后的余数相同,余数为 1 和 2,因此读数错误。 任何帮助,将不胜感激。谢谢!
尝试过更改和迁移注册商,但无济于事。
更新:我能够在屏幕上显示余数,唯一的问题是在除以 7 时获得适当的调用,以便它仅在给出值 7、8、9 时调用 'isdiv'。
bits 16
org 0x100 ; start offset at memory position 100
jmp main ; jump to main program
;
; Data definitions
;
mess1: db 'Input any number (1 - 9)', 0dh,0ah,'$'
mess2: db 'The number is divisible by 7',0dh,0ah,'$'
mess3: db 'The number is not a divisible by 7',0dh,0ah,'$'
mess4: db 'The remainder is: ',0ah, 0dh,'$' ;Not sure
errmess: db '**',0dh,0ah,'$'
crlf: db 0dh,0ah, '$'
;
; Display a string on the screen
; DX contains the address of the string
;
display:
mov ah,09
int 21h
ret
;
;Display the remainder
remainder:
mov dx,mess4
call display
ret
; Set the cursor position
;
cursor:
mov ah,02
mov bh,0 ; screen number mov
mov dh,05h ; row
mov dl,0 ; column
int 10h
ret
;
; Display a user prompt
;
prompt:
mov dx,mess1
call display
ret
;
; Read one character from the keyboard
;
input:
mov ah,01
int 21h
ret
;
; Clear screen and change screen colour
;
screen:
mov ah,06 ; scroll up screen
mov al,0 ; lines to scroll where 0 clear entire screen
mov cx,0 ; starting row:column
mov dl,80 ; ending row;column
mov dh,80
mov bh,00011110b ; colour: yellow on blue
int 10h
ret
;
; Carriage returnm and line feed
;
newline:
mov dx,crlf
call display
ret
;
; Main program
;
main:
call screen
call cursor
next:
call prompt
call input
cmp al,'1' ; character < 1?
jl error ; yes, error message
cmp al,'9' ; character > 9?
jg error ; yes, error message
sub al,30h ; convert from ASCII to numeric
xor ah,ah ; clear AH
mov bl,7
idiv bl ; divide by 7
mov ch,ah
;cmp ah,0 ; remainder = n0?
je isdiv ; yes: divisible by 7
call newline
mov dx,mess3 ; not divisible by 7
call display
jmp fin
isdiv:
call newline
mov dx,mess2
call display ; divisible by 7
call remainder
add ch,30h
mov dl,ch
mov ah,2h
int 21h
fin:
;
int 20h ; terminate program
; Display error message. Number out of range
;
error:
mov dx,errmess
call display
jmp next
添加了 mov cl,al ;复制初始输入供以后在 cmp cl,7 中使用,并使用 jge isdiv 显示余数。 这解决了我的问题。
idiv bl ; divide by 7 mov ch,ah ;cmp ah,0 ; remainder = n0? je isdiv ; yes: divisible by 7
idiv
指令没有定义任何标志。如果不显式地将 AH
与零进行比较,条件跳转将无法正常执行。
当代码发现输入的数字不能被7整除时,它不应该跳到最后(jmp fin
),而是跳到一个你可以决定是否要求显示余数的地方。
您只想查看输入“7”、“8”和“9”的余数。
您可以轻松地过滤掉这些,因为对于较小的输入“1”到“6”,商将是 1 而不是 0!
sub al, 30h ; convert from ASCII to numeric
cbw
mov bl, 7
div bl
mov cx, ax ; SAVE for parts 1 and 2 of the task
call newline
test ch, ch ; Remainder saved earlier
jz isdiv ; Divisible by 7
mov dx, mess3 ; Not divisible by 7
call display
jmp part2
isdiv:
mov dx, mess2
call display
part2:
cmp cl, 1 ; Quotient saved earlier
jne fin ; Input was "1", "2", ... , "6"
mov dx, mess4
call display
mov dl, ch ; Remainder saved earlier
add dl, 30h
mov ah, 02h
int 21h
call newline
fin:
int 20h
mess4: db 'The remainder is: ',0ah, 0dh,'$' ;Not sure
冒号 : 表明您希望余数显示在 后面 文本。最好放下回车 return 然后换行。您可以在之后调用单独的 newline。
mess4: db 'The remainder is: $'
screen: mov ah,06 ; scroll up screen mov al,0 ; lines to scroll where 0 clear entire screen mov cx,0 ; starting row:column mov dl,80 ; ending row;column mov dh,80 mov bh,00011110b ; colour: yellow on blue int 10h ret
80x25 文本屏幕以 DL=79
列和 DH=24
行结束。