【发布时间】:2016-03-25 22:09:43
【问题描述】:
我正在为一个班级项目工作,它按照规则的要求工作,但我想知道是否有更好的方法来实现一些东西。在另一个项目中,我因一个不必要的“mov”而被扣了几分。这是问题1。
"if—else(34 分):编写一个程序,要求用户输入一个数字。如果该数字小于 5,您将声明它,并将其添加 5 并将其存储在变量中;如果数字大于5,则声明so,然后减去5并将其存储在变量中;如果数字为5,则将其加3并声明so并将其存储在变量中。”
org 100h
mov dx, offset start ;move start of string address 'start' into dx
mov ah, 09h
int 21h ;print the string stored at DS:DX
mov ah, 01h ;function for getting keyboard input
int 21h
sub al, 30h ;subtract 30h to store our number as hexadecimal
mov bl, al ;copying data to BL as the following commands manipulate the data
;at AL.
cmp bl, 5 ;BL = AL
jz ifZero ;jump to ifZero if BL = 5
jl ifLess ;jump to isLess if BL < 5
jg ifGreater ;jump to ifGreater if BL > 5
ifZero: ;direct console output function
mov ah, 06h
mov dl, 0Ah
int 21h
mov dl, 0Dh
int 21h ;print newline and character return
add bl, 03h ;add 3 to BL, BL = 8
mov temp, bl
mov dx, offset eq ;move start of string address 'eq' into dx
mov ah, 09h
int 21h ;print string
jmp exit ;unconditional jump to end program
ifLess: ;direct console output function
mov ah, 06h
mov dl, 0aH
int 21h
mov dl, 0Dh
int 21h ;print newline and character return
add bl, 05h ;add 5 to BL
mov temp, bl
mov dx, offset less ;move start of string address 'less' into dx
mov ah, 09h
int 21h ;print string
jmp exit ;unconditional jump to end program
ifGreater:
mov ah, 06h
mov dl, 0ah
int 21h
mov dl, 0dh
int 21h ;print newline and character return
sub bl, 05h ;subtract 5 from BL
mov temp, bl
mov dx, offset great ;move starting address of string 'great' into dx
mov ah, 09h
int 21h ;print string
jmp exit ;unconditional jump to end program
exit:
ret
temp db ?
start db "Please enter a number: $"
less db "Less than 5... adding 5 $"
great db "Greater than 5... subtracting 5 $"
eq db "Equal to 5... adding 3 $"
在这种情况下,是否不需要“mov bl, al”?运行反汇编程序显示 AL 中的数据在大多数这些命令之后发生了变化。这应该发生吗?有更好的方法吗?
问题 3: 计数器控制回路。程序会要求用户输入一个字符,然后它会显示 那个带有五次标签的字符 例如: 输入一个字符:A 您输入:A
org 100h
mov cx, 05h ;counter controlled loop, start as 5
LabelLoop:
mov dx, offset prompt ;move string offset to dx
mov ah, 09h ;function for printing string from dx
int 21h
mov ah, 01h ;function to read character from keyboard
int 21h
mov bl, al ;preserving character read by copying to BL
;as register data for AL will be changing
;due to various functions
mov ah, 06h ;function for direct console output
mov dl, 0ah
int 21h
mov dl, 0dh ;these just make the text appear on a new
;line
int 21h
mov dx, offset output ;move the memory offset of output to dx
mov ah, 09h ;printing another string
int 21h
mov ah, 02h ;function to write a character to console
;gets the value from DL
mov dl, bl ;so we copy BL to DL and print it
int 21h
jmp newLine ;we unconditionally jump to the newLine
;label and print a new line for the program
;to run again
newLine:
mov ah, 06h
mov dl, 0ah
int 21h
mov dl, 0dh
int 21h
loop LabelLoop ;we jump to LabelLoop and CX = CX - 1
mov dx, offset goodbye
mov ah, 09h
int 21h
ret
prompt db 'Enter a character: $'
output db 'You entered: $'
goodbye db 'Good bye!$'
那么,对于这些问题,我的问题是,有没有更好的方法来做到这一点?键盘输入存储在 AL 中,但是每次我对 AH 执行 mov 函数时,寄存器值都会发生变化,无论是字符串打印还是字符打印。为了避免变量(因为它不是要求的一部分)或将其分配到内存(我们还没有学到),我将数据移动到不同的寄存器。这对任何一个程序来说都是不必要的“mov”吗?
编辑:我意识到 AL = DL after
mov ah, 06h
mov dl, 0ah
int 21h ;AL = DL after execution
【问题讨论】:
-
请在codereview.stackexchange.com 发帖,您会得到更好的答案,因为该网站专门针对此类问题,因此它们作为工作程序增强功能不在主题范围内。
-
将您的说明从标签向右缩进一级,以便轻松查看分支标签。我喜欢将操作数缩进到同一个制表位,而不是由于可变长度的 insn 助记符而使操作数列参差不齐。 (我也喜欢将跳转目标缩进比其他操作数略少,因此分支和分支目标一样突出)。 x86 tag wiki 有一个关于格式化 asm 的链接。顺便说一句,您在评论格式方面做得很好,对内容也很好。通常避免描述 insn 的作用,只说它的用途。