Assembly 程序集x86返回(ret)不工作

Assembly 程序集x86返回(ret)不工作,assembly,x86,emu8086,Assembly,X86,Emu8086,最近我开始学习汇编8086,主要是出于好奇 汇编中的输入只允许您键入一个字符,因此我尝试在汇编8086中制作一个程序,允许您输入多位数整数输入,以“空格”(“”)结束输入,然后添加数字并打印值 我看到可以使用push和pop将参数传递给过程,但我尝试使用它们使过程返回一些内容并将其存储到变量中,根据我对汇编8086的了解,我无法想象使用ret实现这一点的方法,所以…无论如何,我制作了一个过程,但是由于某些原因,过程末尾的ret似乎不起作用,并且过程运行了无限次 到目前为止的代码: .model

最近我开始学习汇编8086,主要是出于好奇

汇编中的输入只允许您键入一个字符,因此我尝试在汇编8086中制作一个程序,允许您输入多位数整数输入,以“空格”(
“”
)结束输入,然后添加数字并打印值

我看到可以使用
push
pop
将参数传递给过程,但我尝试使用它们使过程返回一些内容并将其存储到变量中,根据我对汇编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
.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
。型号小
组织100小时
.数据
fv-db-0;用于存储数字的变量
svdb0;
.代码
jmp启动;我的一个模式在某种程度上避免了一个过程被调用两次以及每个人都显示的内容
;只是不起作用,在主进程结束后,所有进程都会重新运行,
;在我使用程序的任何其他时候,它都能完美地工作
f1程序;程序
mov-cl,0;清除cl bcs后第一次使用时我没有在那里存储一些东西
;总是出于某种原因,对于我的所有程序,“cx”都存储了一个值,可能来自
;我使用的模拟器
mov-ah,1h;单字符输入
int 21h;
而:
化学机械抛光铝;如果输入等于“space”(“space”)就我所知,必须是最后一个输入
;随节目一起去)
真的;如果输入!=''
je假;如果输入=“”
对:;以防输入!=''
mov-bl,al;存储输入
mov al,cl;以前输入的数字
亚bl,30小时;bcs如果输入=8,则实际存储的是其在本文件中的ASCII码
;案例:38小时或56小时
mov-dl,10;我的想法是:如果用户写的是输入12,那么实际上是什么类型的1*10+2
;因此,我将10存储到dl中,以将先前输入的数字相乘
;上面的例子:1
mul-dl;乘法
添加al,bl;将新输入添加到(旧输入*10)
mov-cl,al;存储旧输入
mov-ah,1h;输入
int 21h;
jmp-while;如果输入=='',请再次检查
错:;如果输入=“”
mov-ch,0;在chase中,ch有一些其他的东西,而不是

; 输入(0嗯,我找到了它,在我将任何其他内容推到堆栈之前(我搜索了how
push
pop
call
ret
工作)我
pop
-ed进入
bx
,然后在我
push
-ed进入堆栈之后,在
ret
之前,我
push
-ed进入
bx
(地址
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
.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

最好用寄存器而不是堆栈传递值。@Mike我也这么想,但我只是想“玩”使用堆栈并给我自己一些挑战可能会有帮助:问题是
调用
ret
也使用堆栈。要从函数返回,通常使用
ax
寄存器。要将参数传递到函数中,通常将它们推到堆栈上,使用
bp
寄存器作为ex访问它们我在上面的链接中解释了这一点,然后使用
ret XX
自动弹出它们,返回后弹出的字节数。不过,还有许多其他方法可以传递数据。查看维基百科文章,了解一下^^