汇编语言x8086 - 传递参数

时间:2014-11-07 07:58:48

标签: assembly parameter-passing procedure

我需要一些帮助来弄清楚如何传递我已声明为参数的变量。我的教授根本没有解释清楚,当我问他一个问题时,他只是告诉我读这本书,但我的问题是因为我不完全理解这本书。

基本上,在这个家庭作业中,我必须将参数传递给我的程序,例如传递我的变量:userMsg,printSum,sumMessage,printW,wMessage等。我目前对如何开始无能为力。

我不确定如何将String作为参数传递,所以当另一个查看我的代码的人可以修改主过程和声明中的变量名并使程序运行完美时。

这是我到目前为止所做的:

.model small
.stack 100h
.data

Y dw ? 
W dw ?
Sum dw ?   
Space db ,0ah, '$'
userMsg db "Please enter your number: ", '$'   
printSum db "The Sum is: ",  
sumMessage db 0,0,0,0,0     ,0dh, 0ah
printW db "W is: " 
wMessage db 0,0,0,0,0   ,0dh, 0ah, '$'  


.code
main proc
mov ax,@data
mov ds,ax   


call getInput 
    call calcInput
call modifySum    
call displaySum

mov ax,4C00h
int 21h

main endp 




;getInput procedure
getInput proc


mov si, 0
mov cx, 100

yLoop:                              ;Loop to print message asking user to input number
    mov al, 0
    mov al, userMsg[si]
    inc si 
    cmp al, '$'
    je endY
    mov dl, al   
    mov ah, 2h
    int 21h  

loop yLoop  

endY: 
    mov si, 0
    mov cx, 0

    mov     cx, 10
    mov     bx, 0  
loop1:
    mov     ah,1h
    int     21h   
    cmp     al,0dh 
    je      endloop  
    sub al, 30h
    mov ah, 0h
    mov si, ax
    mov ax, bx   
    mul cx
    mov bx, ax
    add bx,si
    jmp     loop1
endloop: 


ret   

getInput endp 


;calcInput procedure
calcInput proc

mov Y, bx

mov ax,Y     ;Store Y in ax register
sub ax,1
mov Y, ax
mov ax, 0

mov Sum,36         ; add 36 to Sum
mov bx,Y
add Sum,bx         ; add 36 and Y into Sum
mov ax,Y

mov bx,4            ; take Y and divide by 4
mov dx,0
idiv bx
add Sum,ax  

mov ax,Y           ;take Y and divide by 100
mov bx,100
mov dx,0
idiv bx
add Sum,ax  

mov bx,7
mov dx,0                        ; calculate W
idiv bx
mov W,dx
add W,1
mov dx, W 

ret             


calcInput endp   


;modifySum procedure
modifySum proc


add dl, 30h
mov wMessage+1, dl  

mov ax, 0
mov dx, 0 
mov ax,Sum  
mov cx, 10            ;start modding the number 2553
idiv cx   

mov si, 4

sumLoop:                   ;Loop to mod and store 2553 into sumMessage
     add dl, 30h
     mov sumMessage+[si], dl 
     mov dx, 0 
     mov cx, 10 
     idiv cx
     dec si   
     cmp si, 0h
     je endSum

loop sumLoop  

endSum: 

mov si, 1

removeZero:                 ;Locate zeros

    cmp sumMessage+[si], 30h
    je useSpace  
    jmp endRemove

loop removeZero              

useSpace:                       ;Replace leading zeros with a blank space

    mov sumMessage+[si], 20h 
    inc si
    jmp removeZero


loop useSpace 

endRemove:

ret           

modifySum endp  


;displaySum procedure
displaySum proc


mov ax, 0               ;Print a newline after user input
mov dl, Space
mov ah, 2h
int 21h 




mov si, 0
mov cl, printSum

L1:                              ;Loop to print out "Sum is : 2553
    mov al, 0
    mov al, printSum[si]
    inc si 
    cmp al, '$'
    je end_loop
    mov dl, al   
    mov ah, 2h
    int 21h  

loop L1:  

end_loop:

ret   


displaySum endp

end main

提前感谢您的帮助!

1 个答案:

答案 0 :(得分:1)

基于您的代码的示例。

        mov     ax, Y
        lea     bx, userMsg
        push    bx
        push    ax
        call    example  ; Y, offset userMsg
        add     sp,4

显示字符串:

example proc
        push    bp                    ;save bp
        mov     bp,sp                 ;set bp = sp
;                                     ;2[bp] = return address
;                                     ;4[bp] = first parameter
;                                     ;6[bp] = second parameter
        mov     dx,6[bp]              ;display message
        mov     ah,09h
        int     21h
;       ...
        pop     bp
        ret