小编use*_*802的帖子

用C和汇编编写一个简单的线程

我正在尝试编写一个简单的用户级线程库作为我的OS课程的练习.作为第一步,我试图运行一个程序并跳转到一个函数离开第一个程序.到目前为止的代码是这样的:

最初的计划:

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <assert.h>

#define STACK_SIZE (sizeof(void *) * 512)


void proc2() //This is the function that should run as the thread.
{
    int i;
    for(i=0;i<30;i++)
    {
        printf("Here I am!\n");
        sleep(0.5);
    }
    exit(0);
}

void* malloc_stack() //used to malloc the stack for the new thread. 
{
    void *ptr = malloc(STACK_SIZE + 16);
    if (!ptr) return NULL;
        ptr = (void *)(((unsigned long)ptr & (-1 << 4)) + 0x10); //size align
    return ptr;
}

int main() …
Run Code Online (Sandbox Code Playgroud)

c assembly x86-64

9
推荐指数
1
解决办法
647
查看次数

理解C反汇编的电话

我想学习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)

c assembly x86-64 calling-convention att

6
推荐指数
2
解决办法
6309
查看次数

分段错误使用C和程序集创建用户级线程

我试图通过一些任务来理解一些操作系统基础知识.我已经发布了一个类似的问题,得到了令人满意的答案.但这个略有不同,但我无法调试它.所以这就是我的工作:

我想要做的是启动一个主程序,malloc一个空间,用它作为一个堆栈来启动用户级线程.我的问题是返回地址.这是迄今为止的代码:

[我正在编辑我的代码,使其与我的答案的当前状态保持同步]

#include <stdio.h>
#include <stdlib.h>
#include <assert.h>

#define STACK_SIZE 512

void switch_thread(int*,int*);

int k = 0;

void simple_function()
{
    printf("I am the function! k is: %d\n",k);
    exit(0);
}

void create_thread(void (*function)())
{   
   int* stack = malloc(STACK_SIZE + 32);
   stack = (int* )(((long)stack & (-1 << 4)) + 0x10);
   stack = (int* ) ((long)stack + STACK_SIZE); 
   *stack = (long) function;
   switch_thread(stack,stack);  
}

int main()
{
    create_thread(simple_function);
    assert(0);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

switch_thread是我编写的汇编代码如下:

.text
    .globl  switch_thread
switch_thread:  
    movq    %rdi, %rsp
    movq …
Run Code Online (Sandbox Code Playgroud)

c assembly

5
推荐指数
1
解决办法
492
查看次数

C中的后增量运算符

我编写这个C代码时,我随便编码:

#include <stdio.h>
int main()
{
    int i;
    i = 10;
    printf("i : %d\n",i);
    printf("sizeof(i++) is: %d\n",sizeof(i++));
    printf("i : %d\n",i);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

当我运行代码时,我得到的结果是,

i : 10
sizeof(i++) is: 4
i : 10
Run Code Online (Sandbox Code Playgroud)

我对这个结果感到困惑,因为我预计i ++内的sizeof运算符会增加i.但似乎没有.因此,出于好奇,我写了以下程序:

#include <stdio.h>
int  add(int i)
{
    int a = i + 2;
    return 4;
}
int main()
{
    int i;
    i = 10;
    printf("i : %d\n",i);
    printf("sizeof(i++) is: %d\n",add(i++));
    printf("i : %d\n",i);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

对于这个程序,输出是:

i : 10
sizeof(i++) is: 4
i : 11 …
Run Code Online (Sandbox Code Playgroud)

c post-increment

4
推荐指数
2
解决办法
190
查看次数

标签 统计

c ×4

assembly ×3

x86-64 ×2

att ×1

calling-convention ×1

post-increment ×1