我正在尝试将内存动态分配到堆中,然后在这些内存地址中分配值。我了解如何分配内存,但我如何将例如寄存器中的值分配给第一个动态内存地址?这是我到目前为止:
push rbp
mov rbp, rsp ;initialize an empy stack to create activation records for the rest of the subroutines
mov rax, 0x2d ;linux system call for brk()
mov rbx, 0x0 ;to get the adress of the first adress we are allocating we must have 0 in rbx
int 0x80 ;calls the linux operating system kernel for assistance
mov [brk_firstLocation], rax ;the first position in the heap will be returned in rax thus i save the first loaction in a …Run Code Online (Sandbox Code Playgroud) Linux 如何通过系统调用确定要执行的另一个进程的地址?就像在这个例子中一样?
mov rax, 59
mov rdi, progName
syscall
Run Code Online (Sandbox Code Playgroud)
我的问题似乎有点混乱,澄清一下,我问的是系统调用是如何工作的,与传递的寄存器或参数无关。当另一个进程被调用时,它如何知道跳转、返回等位置。
最近我在玩 freebsd 系统调用,我对 i386 部分没有问题,因为它在这里有很好的记录。但是我找不到 x86_64 的相同文档。
我看到人们在 linux 上使用相同的方式,但他们只使用程序集而不是 c。我想在我的例子中,系统调用实际上改变了一些被高优化级别使用的寄存器,所以它给出了不同的行为。
/* for SYS_* constants */
#include <sys/syscall.h>
/* for types like size_t */
#include <unistd.h>
ssize_t sys_write(int fd, const void *data, size_t size){
register long res __asm__("rax");
register long arg0 __asm__("rdi") = fd;
register long arg1 __asm__("rsi") = (long)data;
register long arg2 __asm__("rdx") = size;
__asm__ __volatile__(
"syscall"
: "=r" (res)
: "0" (SYS_write), "r" (arg0), "r" (arg1), "r" (arg2)
: "rcx", "r11", "memory"
); …Run Code Online (Sandbox Code Playgroud) 那就是你怎么知道的
特定系统调用所需的参数数量,
注册每个参数应该在,
最后每个参数意味着什么?
是否有man相似的命令告诉你?
我试图从c调用汇编函数,但我不断收到错误.
.text
.globl integrate
.type integrate, @function
integrate:
push %ebp
mov %esp, %ebp
mov $0,%edi
start_loop:
cmp %edi,1024
je loop_exit
mov 8(%ebp),%eax
mov 12(%ebp),%ecx
sub %eax,%ecx
add %edi,%ecx
incl %edi
jmp start_loop
loop_exit:
movl %ebp, %esp
popl %ebp
ret
Run Code Online (Sandbox Code Playgroud)
这是我的汇编函数,名为integrate.s的文件.
#include <stdio.h>
extern int integrate(int from,int to);
void main()
{
printf("%d",integrate(1,10));
}
Run Code Online (Sandbox Code Playgroud)
继承人我的代码.
function.c:5:6: warning: return type of ‘main’ is not ‘int’ [-Wmain]
/tmp/cciR63og.o: In function `main':
function.c:(.text+0x19): undefined reference to `integrate'
collect2: ld returned 1 exit status
Run Code Online (Sandbox Code Playgroud)
每当我尝试使用gcc -Wall …
我想学习C调用约定.为此,我编写了以下代码:
#include <stdio.h>
#include <stdlib.h>
struct tstStruct
{
void *sp;
int k;
};
void my_func(struct tstStruct*);
typedef struct tstStruct strc;
int main()
{
char a;
a = 'b';
strc* t1 = (strc*) malloc(sizeof(strc));
t1 -> sp = &a;
t1 -> k = 40;
my_func(t1);
return 0;
}
void my_func(strc* s1)
{
void* n = s1 -> sp + 121;
int d = s1 -> k + 323;
}
Run Code Online (Sandbox Code Playgroud)
然后我使用GCC使用以下命令:
gcc -S test3.c
Run Code Online (Sandbox Code Playgroud)
并想出了它的装配.我不会显示我得到的整个代码,而是粘贴函数my_func的代码.就是这个:
my_func:
.LFB1:
.cfi_startproc
pushq %rbp
.cfi_def_cfa_offset 16 …Run Code Online (Sandbox Code Playgroud) 前几天我决定在组装时采取行动,我一直在玩基本的东西,比如从argv到stdout的打印.我找到了这个带有参数和所有内容的linux系统调用号码的绝佳列表,我很好奇为什么r10之前r8和之前用于参数r9.我已经找到各种奇怪的约定,关于什么可以用于什么和何时,如循环计数器如何进入rcx.有什么特别的原因r10被提升了吗?它更方便吗?
我可能也应该提到我出于好奇而对此感兴趣,而不是因为它引起了我的问题.
编辑:我发现这个问题很接近,引用了第124页的x64 ABI文档,其中注意到用户级应用程序使用的问题rdi, rsi, rdx, rcx, r8, r9.另一方面,内核使用r10而不是rcx破坏rcx和r11.这可能解释了如何r10在那里结束,但那么为什么要交换呢?
我知道在Linux x64中,“ syscall”和“ int 0x80”汇编程序指令会在软件中生成中断,要求内核执行某些工作。它们具有不同的操作码(0F 05与CD 80),并且前者速度更快。
我不清楚它们之间是否存在任何关系:它们真的独立吗?(即:“ syscall”是否调用“ int 0x80”?)谢谢。
我试图学习汇编语言作为一种爱好,我经常用它gcc -S来产生汇编输出.这非常简单,但我无法编译汇编输出.我只是好奇这是否可以完成.我尝试使用标准汇编输出和intel语法-masm=intel.两者都无法编译nasm和链接ld.
因此,我想问一下是否可以生成汇编代码,然后可以编译.
更准确地说,我使用了以下C代码.
>> cat csimp.c
int main (void){
int i,j;
for(i=1;i<21;i++)
j= i + 100;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
生成程序集gcc -S -O0 -masm=intel csimp.c并尝试编译nasm -f elf64 csimp.s和链接ld -m elf_x86_64 -s -o test csimp.o.我从nasm得到的输出读取:
csimp.s:1: error: attempt to define a local label before any non-local labels
csimp.s:1: error: parser: instruction expected
csimp.s:2: error: attempt to define a local label before any non-local labels
csimp.s:2: error: parser: …Run Code Online (Sandbox Code Playgroud) 如果您正在编写程序集,那么将值分配给哪个寄存器重要吗?假设您将累积/中间值存储在 %ebx 而不是 %eax 中,后者传统上用于此目的。这是不好的做法吗?会影响性能吗?
换句话说,您是否可以将它们等同于存储空间,还是应该坚持将它们用于特定目的?