如何在另一个子程序中调用子程序?

问题描述 投票:1回答:1

我最近开始研究一个项目,该项目将用户输入的Postfix表达式转换为前缀和中缀表达式。但是,我遇到了在其他子程序中调用这些子程序的问题,因为我不确定它们是如何运行的。我想做的是在postfix_to_prefix子程序中调用strcat子程序,但对于我的生活,我无法弄清楚如何去做。

这个项目用于计算机体系结构课程,到目前为止我花了大约10个小时试图对如何编写程序进行个人研究,但是我没有找到我必须做的事情才能使它正常运行,特别是代码的操作标签。

; A Subprogram that converts Postfix Prefix
segment .data
plus_sign   db ' + ', 0
minus_sign  db ' - ', 0
multiply    db ' * ', 0
divider     db ' / ', 0
parenthsf   db ' ( ', 0
parenthsb   db ' ) ', 0

segment .bss    
operand     resd 1
operators   resd 1

segment .text


postfix_to_infix:
    push    ebp
    mov     ebp, esp

    mov     eax, 0
    mov     esi, 0
    mov     edi, 0
    mov     ebx, [ebp + 8]                      ; ebx = infix
    mov     ecx, [ebp + 16]                     ; ecx = post_len
    mov     edx, [ebp + 12]                     ; edx = postfix

comparison:
    mov     al, [edx + esi]
    cmp     al, '+'
    je      operation
    cmp     al, '-'
    je      operation
    cmp     al, '*'
    je      operation
    cmp     al, '/'
    je      operation
    cmp     al, ' '
    je      opexit

    inc     esi
    push    eax
    loop    comparison


operation:
    mov     [operators], eax
    pop     eax
    mov     [operand], eax
    pop     eax

    ;This point here is what I specifically need to figure out...
    pusha
    push    dword[operand]  
    push    eax
    call    strcat
    add     esp,8
    popa


opexit:
    inc     esi
    loop    comparison

    pop ebx
    pop ebp
    ret



; A subprogram strcat appends the contents of one string to the end of another
; strcat(str1,str2)
; Result: str1= str1 + str2
; stored in str1
segment .bss
append_length   resd 1
read_length     resd 1
segment .text
strcat: 
    push    ebp                             ; 
    mov     ebp, esp                        ;
    mov     edx, [ebp+12]                   enter code here; what we want to append

    push    edx                             ;
    call    length_is                       ;
    add     esp, 4                          ;
    mov     [append_length], eax            ;
    mov     eax, 0                          ;

    mov     ebx, [ebp+8]                    ; original string str1

    push    ebx                             ;
    call    length_is                       ;
    add     esp, 4                          ;
    mov     [read_length], eax              ;
    mov     eax, 0                          ;

    mov     ebx, [ebp+8]                    ; original string str1
    mov     edx, [ebp+12]                   ; what we want to append
    mov     ecx, [append_length]            ;
    mov     edi, 0                          ;
    mov     esi, [read_length]              ; the last location of original string is where to append 

append_loop:
    mov     al, [edx+edi]                   ;
    mov     [ebx+esi], al                   ;
    add     esi, 1                          ;
    add     edi, 1                          ;
    loop    append_loop                     ;

    pop     ebp                             ;
    ret 

该程序旨在将postfix转换为前缀并将其显示给用户。我需要帮助搞清楚如何调用这些子程序或如何使用它们以获得正确的输出。

assembly x86 nasm function-call
1个回答
0
投票

显然,如果你在函数调用周围使用pusha / popapopa会破坏EAX中的返回值,将其替换为调用前EAX中的任何内容。不要那样做。

让函数破坏call-clobbered寄存器(i386 System V调用约定1中的EAX,ECX和EDX),并将这些寄存器用于临时或任何不需要在函数调用中生存的寄存器。


脚注1:几乎所有其他32位x86调用约定。但是如果您想查找有关如何传递args的更多详细信息(例如,获取结构的函数),您可以查看C编译器输出,或查找i386 System V ABI文档。

© www.soinside.com 2019 - 2024. All rights reserved.