【问题标题】:16 bit Assembly Program16 位汇编程序
【发布时间】:2013-09-24 17:41:34
【问题描述】:

所以我开始在 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。 然后在 div bl 部分,al 除以 bl 为 3,余数存放在 dx 中,如果与 0 比较,发现大于则跳转到 notEqual 部分,否则跳转到 end 部分。

就像现在一样,我总是显示 finalMsg,只有当值完全可以被 3 整除时才应该显示它。

任何人都有一些建议。 谢谢。 JP.

【问题讨论】:

  • 打印finalErrorMsg 后,您不会终止程序。因此,它还会在此之后打印finalMsg。快速修复:在到达end 标签之前跳转到loop1

标签: assembly nasm division 16-bit


【解决方案1】:

你正在做一个div bl,它被一个字节除。所以商在al 中,余数在ah 中,而不是在axdx 中,正如您的代码所假设的那样。确保在div 之前清除ah,因为您的股息是al 中的单个字节。

【讨论】:

  • 好的,所以我将比较语句更改为: cmp ah, 0 但是,它现在总是跳到不能被三个部分整除。这和以前相反。
猜你喜欢
  • 2017-04-03
  • 1970-01-01
  • 2011-12-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-22
  • 2013-12-20
相关资源
最近更新 更多