嗯,最近我开始学习Assembly 8086主要是出于好奇。
程序集中的输入只允许您键入一个字符,因此我尝试在程序集 8086 中制作一个程序,允许您输入多位数整数输入,以"space"(' '
)结束输入,然后添加数字并打印值。
我看到push
和pop
可用于将参数传递给过程,但我试图使用它们让我的过程返回一些东西并将其存储到变量中,我无法想象一种方法可以做到这一点ret
基于我对程序集 8086 的了解,所以......无论如何,我做了一个程序,但由于某种原因,程序末尾的ret
似乎不起作用,程序运行无限次。
到目前为止的代码:
.model small
org 100h
.data
fv db 0 ;variables to store the numbers
sv db 0 ;
.code
jmp start ;a pattern of mine in some way to avoid a procedure be called twice and what everyone shows
;just doesn't work, after the main ends, all the procs run again,
;it worked perfectly any other time I used procedures to my program
f1 proc ;the procedure
mov cl, 0 ;clear cl bcs later the first time is used I have not stored some thing in there and
;always for some reason, to all my programs "cx" has a value stored, maybe from the
;emulator I use
mov ah, 1h ;single character input
int 21h ;
while:
cmp al, ' ' ;if input is equal to "space"("space" must be the last input, as far as I have
;gone with the program)
jne true ; if input != ' '
je false ; if input == ' '
true: ; in case input != ' '
mov bl, al ;store the input
mov al, cl ;digits taken from input previously
sub bl, 30h;bcs if input == 8, whatactually is stored is the ASCII code of it in this
;case : 38h or 56
mov dl, 10 ;What I thought : if user writes as input 12, what actually types 1 * 10 + 2
;so I am storing 10 to dl to multiply the previously entered numbers in the
;example above : 1
mul dl ;multiplication
add al, bl ;add new input to (old inputs * 10)
mov cl, al ;store old inputs
mov ah, 1h ;input
int 21h ;
jmp while ;check again if input == ' ' or not
false: ;in case input == ' '
mov ch, 0 ; in chase ch had some thing else in it from something else than the
; input(0 <= input <= 127~128(127 + 128 = 255))
push cx ; store cx(final result from the inputs) in to the stack to store it to a
; variable
ret ; end procedure
f1 endp ;
start: ; with "jmp start" at the start of the ".code" makes the program start from "main"
main proc
call f1 ;call procedure
pop bx ;store result in to bx bcs `push` and `pop` as far as I know need at least 16-bit
;and varables are 8-bit, I know I can make them 16-bit but anyway
mov fv, bl ;store result to variable
endp
end main
好吧,我找到了它,在我将其他任何东西推送到堆栈之前(我搜索了push
、pop
、call
和ret
的工作方式)我pop
-ed 进入bx
,然后在我push
-ed 到堆栈之后我想push
,在ret
之前,我push
-ed 要bx
的内容(ret
应该跳跃的地址),然后我ret
.
法典:
.model small
org 256
.data
fv db 0
sv db 0
.code
jmp start
f1 proc
mov cl, 0
mov ah, 1h
int 21h
while:
cmp al, ' '
jne true
je false
true:
mov bl, al
mov al, cl
sub bl, 30h
mov dl, 10
mul dl
add al, bl
mov cl, al
mov ah, 1h
int 21h
jmp while
false:
pop bx
mov ch, 0
push cx
push bx
ret
f1 endp
start:
main proc
call f1
pop bx
mov fv, bl
endp
end main