【问题标题】:How to use a c function in assembly [duplicate]如何在汇编中使用c函数[重复]
【发布时间】:2021-07-27 08:00:13
【问题描述】:

我在汇编中有一个名为“test”的函数,另一个文件中有一个名为“shift”的子函数和一个名为“shiftLetter”的c函数。我的 shiftLetter 函数有两个参数,一个带有要移位的字母的字符和一个表示移位次数的整数。 我正在尝试向我的“shift”函数发送消息,该函数调用我的 c 函数来更改此消息中的每个字母。但是,我无法完成这项工作:

test.asm

extern printf, scanf
extern shift
global _start

section .data

format3 db "%s ",0
message1:     db "This is the Message: ",10,0

original: db "Hello World.",10,0

len1:  equ  $ - original

section .text
        global  main


main:

        mov rsi, message1
        call messages


        mov rdi,original           ;message to change
        mov rsi, len1              ;size of message
        mov rbx, 3                 ;number of shifts
        call shift

        mov rsi, original
        call messages

        ret

; end main                                                                                                                                                                                                  



messages
        mov rdi,format3
        mov rdx,rsi
        mov rax,0
        call printf
        ret

shift.asm

global shift
extern shiftLetter

section .text

shift:

        mov rdx, rsi            ;rdx length of string                                                                                                                                                       

        mov rcx,0
loop:   cmp rcx, rdx
        jge done

        mov rax,  byte [rdi+rcx]    ;getting letter to change (I am sure this is wrong)
        mov r8, rbx             ;number of shifts                                                                                                                                                           
        call shiftLetter
        mov byte [rdi+rcx], rax
        inc rcx

        jmp loop

done:
        ret



【问题讨论】:

  • 这是用于什么操作系统/编译器的,您研究过它的调用约定吗?
  • “我无法完成这项工作” - 问:这到底是什么意思???问:您能否成功链接您的 C 和 ASM 对象?问:您使用的是什么 C 编译器?你在运行什么操作系统?建议:使用 -S (GCC/Linux) 或 /Fa (MSVS/Windows) 进行编译以研究编译器生成的“程序集”。
  • @paulsm4 是的,我正在链接它们。我想。对不起,我对装配很陌生。我需要了解如何使用我的 c 函数。就像我如何一次将一个字符发送到我的函数,然后当它返回时我如何将这个新字母存储在我的消息中
  • Without any error messages or details 关于您的minimal reproducible example 会发生什么,我们所能做的就是将问题关闭为无法回答。 (或者作为问题可能是什么的猜测,请参阅副本。)

标签: assembly x86-64 calling-convention


【解决方案1】:
  1. 细节会因架构(例如 x86 与 x86-64 与 MIPS)、平台(例如 Windows 与 Linux 与 MacOS)以及编译器(例如 GCC 与 MSVS)而异。

  2. 最好的办法是编写一个小型 C 程序并使用 C 编译器的“转储到程序集”命令开关。例如:

test.c:

#include <stdio.h>

void test()
{
  char buf[10];
  buf[0] = 'A';
  buf[1] = 0;
  printf("%s\n", buf);
}

GCC“生成程序集”命令行:

gcc -S -O0 x.c
ls -l x.*
-rw-r--r-- 1 root root 105 May  4 19:42 x.c
-rw-r--r-- 1 root root 598 May  4 19:43 x.s

x.s(等效汇编代码):

        .file   "x.c"
        .text
        .globl  test
        .type   test, @function
test:
.LFB0:
        .cfi_startproc
        pushq   %rbp
        .cfi_def_cfa_offset 16
        .cfi_offset 6, -16
        movq    %rsp, %rbp
        .cfi_def_cfa_register 6
        subq    $32, %rsp
        movq    %fs:40, %rax
        movq    %rax, -8(%rbp)
        xorl    %eax, %eax
        movb    $65, -18(%rbp)
        movb    $0, -17(%rbp)
        leaq    -18(%rbp), %rax
        movq    %rax, %rdi
        call    puts@PLT
        nop
        movq    -8(%rbp), %rax
        xorq    %fs:40, %rax
        je      .L2
        call    __stack_chk_fail@PLT
.L2:
        leave
        .cfi_def_cfa 7, 8
        ret
        .cfi_endproc
.LFE0:
        .size   test, .-test
        .ident  "GCC: (Ubuntu 7.5.0-3ubuntu1~18.04) 7.5.0"
        .section        .note.GNU-stack,"",@progbits

您可以在我们初始化“buf[]”的地方认出这一点:

    movb    $65, -18(%rbp)
    movb    $0, -17(%rbp)

还有这个,我们调用 C 运行时库puts()

    leaq    -18(%rbp), %rax
    movq    %rax, %rdi
    call    puts@PLT

您想用不同的 C 代码“试验”,看看它如何在您的特定环境中转换为汇编程序。

【讨论】:

猜你喜欢
  • 2020-11-27
  • 2020-08-11
  • 2020-10-02
  • 2020-06-06
  • 2021-11-11
  • 1970-01-01
  • 1970-01-01
  • 2014-08-20
  • 1970-01-01
相关资源
最近更新 更多