assembly - 如何在汇编中使用 ac 函数
问题描述
我在程序集中有一个名为“test”的函数,另一个名为“shift”的文件中有一个子函数,还有一个名为“shiftLetter”的函数。我的 shiftLetter 函数有两个参数,一个带有要移位的字母的字符和一个表示移位次数的整数。我正在尝试向我的“shift”函数发送消息,该函数调用我的 c 函数来更改此消息中的每个字母。但是,我无法完成这项工作:
测试.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
移位.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
解决方案
细节会因架构(例如 x86 与 x86-64 与 MIPS)、平台(例如 Windows 与 Linux 与 MacOS)以及编译器(例如 GCC 与 MSVS)而异。
最好的办法是编写一个小型 C 程序并使用 C 编译器的“转储到程序集”命令开关。例如:
测试.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
xs(等效汇编代码):
.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 代码“试验”,看看它如何在您的特定环境中转换为汇编程序。
推荐阅读
- reactjs - 为什么我无法从另一台计算机连接到我的 create-react-app 本地开发服务器?
- php - GMail 创建转发解决另一个域范围的委派问题 - PHP
- discord - 不和谐机器人如何在线?
- python - 用猜出的字母代替破折号/ Hangman / Python
- javascript - 返回一个数字在数组中的位置
- android - 如何在 android 片段中为 searchView 添加 QueryTextListener?
- arrays - C中的指针数组,易于迭代
- node.js - 为 module.exports = 函数和一些接口创建 .d.ts
- python - 在字典中查找键并将键的一部分用作变量
- python - 如何设置烧瓶 pymongo 配置以将其与带有 mongo 图像的容器一起使用?