【发布时间】:2017-10-29 00:06:49
【问题描述】:
我是 Assembly 的新手,我正在尝试执行以下操作:
伪代码:
loop:
input
if(input == $)
end loop
else if(input < savedInput)
savedInput = input
;
print savedInput
基本上,它是一个恒定循环,用于检测用户输入并将保存的输入的 ASCII 值与新输入进行比较,如果新输入低于则替换保存的输入。如果输入等于 $ 则结束循环并打印保存的输入。
这是我的代码
.MODEL SMALL
.STACK 100h
.DATA
insertMsg DB 13, 10, 'Introduce un dato: ', 13, 10, '$'
.CODE
main:
mov ax,@data
mov ds,ax ; Set DS to point to the data segment
mov dx,OFFSET insertMsg ; Point to the insertMsg
back:
mov ah,9 ; DOS: print string: Service 21h, 09h
int 21h ; Display inputMsg
mov ah,1 ; DOS: get character: Service 21h, 01h
int 21h ; Get a single-character response
cmp al,'$' ; if character equals $
je display ; goto display
loop back ; loop back
display:
mov ah,9 ;DOS: print string: Service 21h, 09h
int 21h ;display input
mov ah,4Ch ;DOS: terminate program: Service 21h, 4Ch
mov al,0 ;return code will be 0
int 21h ;terminate the program
end main
问题是我不知道如何保存和比较 ASCII 值
【问题讨论】:
-
您已经在进行比较。要存储一个值,只需选择一个未销毁的寄存器。
-
loop back在这种情况下不是一个好的选择,你想做jmp back总是跳到那里,loop在cx变为零后不会跳(你不会定义cx,这样你就可以随意使用启动环境了)。而loop几乎从来都不是好的选择,除非您对代码大小很感兴趣,否则这对指令:dec cxjnz label在现代 x86 CPU 上的性能更好。 -
或者在这种情况下,
jne back而不是有条件地向前跳转到无条件循环分支。