在汇编语言 x86 中,如何在 cmp 之后调用函数?

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

我刚刚习惯了 Assembly 中的功能,我遇到了一个问题。我需要将

array
的正元素重写为
new_array
。在函数
positive
中,我正在使用
cmp
,如果元素为正,我将跳转到
call_write
,调用函数
write
。如您所见,
call_write
没有 ret,但我在 edb-debugger 中注意到它由于某些原因无论如何都会返回。由于
call_write
不是函数,因此
ret
positive
函数返回。

%include "../lib64.asm"

section .data
    InputText db "Input 5 elements by ENTER", 10
    TextLen db $-InputText
    array_size db 5
section .bss
    array resd 5
    new_array resd 5
    Input_buf resd 1
    A resd 1
section .text

global _start


read_cycle:
    cmp ebp, [array_size]
        je return
    mov eax, 0; read
    mov edi, 0; stdin=0
    mov esi, Input_buf; адрес вводимой строки
    mov edx, 4; buf len
    syscall
    
    mov esi, Input_buf
    call StrToInt64
    mov [array + ebp * 2 ], eax  
    
    inc ebp
    loop read_cycle
    ret


write_cycle:
    cmp ebp, [array_size]
        je return
    mov esi, Input_buf
    mov eax, [new_array + ebp * 2]
    call IntToStr64
    
    mov eax, 1;write
    mov edi, 1;stdout=1
    mov esi, Input_buf 
    mov edx, 4 ; длина строки
    syscall 
    
    inc ebp
    loop write_cycle
    ret


modify:   
    mov ebp, 0
    mov ecx, 0
    call positive
    ret


positive:
    cmp ebp, [array_size]
        je return
    mov eax, [array + ebp * 2 ]
    cmp eax, 0
        jge write
    call write
    inc ebp
    loop positive
    ret

write:
    mov eax, [array + ebp * 2]
    mov [new_array + ecx * 2], eax
    inc ecx
    ret

call_write:
    call write

return:
    ret
_start:
    ;input text:
    mov eax, 1; write
    mov edi, 1; stdout = 1
    mov esi, InputText
    mov edx, TextLen
    syscall
    
    mov ebp, 0
    call read_cycle
    call modify
    mov ebp, 0
    call write_cycle
    jmp exit

exit:
    mov rax, 60;
    xor rdi, rdi; return code 0
    syscall

那么,这种行为的原因是什么?我需要如何正确调用 cmp 之后的函数?

另一个实现(仍然不起作用):

positive:
    cmp ebp, [array_size]
        je return
    mov eax, [array + ebp * 4]
    cmp eax, 0
        jge call_write
    after_write:
        inc ebp
        loop positive
        ret

write:
    mov eax, [array + ebp * 4]
    mov [new_array + ecx * 4], eax
    inc ecx
    ret

call_write:
    call write
    jmp after_write

function assembly x86 return
© www.soinside.com 2019 - 2024. All rights reserved.