组装 x86 | return(ret) 不工作
Assembly x86 | return(ret) is not working
嗯,最近我开始学习 汇编 8086 主要是出于好奇。
汇编输入只允许输入一个字符,所以我尝试在汇编8086中编写一个程序,允许输入多位整数,输入以"space"([=12结束=]), 然后添加数字并打印值。
我看到 push
和 pop
可用于将参数传递给过程,但我尝试使用它们来使我的过程 return 成为某种东西并将其存储到变量中,根据我对 Assembly 8086 的了解,我无法想象用 ret
来做到这一点的方法,所以......无论如何,我做了一个程序,但由于某种原因 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
-编辑 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
嗯,最近我开始学习 汇编 8086 主要是出于好奇。
汇编输入只允许输入一个字符,所以我尝试在汇编8086中编写一个程序,允许输入多位整数,输入以"space"([=12结束=]), 然后添加数字并打印值。
我看到 push
和 pop
可用于将参数传递给过程,但我尝试使用它们来使我的过程 return 成为某种东西并将其存储到变量中,根据我对 Assembly 8086 的了解,我无法想象用 ret
来做到这一点的方法,所以......无论如何,我做了一个程序,但由于某种原因 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
-编辑 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