ccccourse / sp110b

課程:系統程式 -- 筆記、習題與報告
1 stars 50 forks source link

習題 3 : 請用 jit 的方法直接執行一個函數的機器碼 #12

Open ccckmit opened 2 years ago

ccckmit commented 2 years ago

函數範例: plus3.c

int plus3(int n) {
    return n+3;
}

編譯

$ gcc -c -fPIC plus3.c -o plus3.o
$ objdump -d plus3.o
....

仿照

https://gitlab.com/ccc110/sp/-/blob/master/06-obj/02-jit/03-objdump/jitAdd.c

寫一個 jitPlus3.c

然後編譯執行

stereomp3 commented 2 years ago

執行結果: image

程式碼


// 修改自 -- https://github.com/spencertipping/jit-tutorial/blob/master/jitproto.c
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <string.h>
#include <sys/mman.h>

typedef int(*fn)(int);

int main() { char *memory = mmap(NULL, // address 4096, // size PROT_READ | PROT_WRITE | PROT_EXEC, MAP_PRIVATE | MAP_ANONYMOUS, -1, // fd (not used here) 0); // offset (not used here) assert(memory != NULL); char code[] = { 0x8d,0x47,0x03,0xc3 };

memcpy(memory, code, 4); fn f = (fn) memory;

printf("plus3(5) = %d\n", (*f)(5)); munmap(f, 4096); return 0; }

jifkavnb0205 commented 2 years ago

110810507 李安博

Lin610313 commented 2 years ago

110910506林庭光 https://github.com/Lin610313/sp110b/wiki/work3.md

Uriel58 commented 2 years ago

https://github.com/Uriel58/sp110b/wiki/%E7%94%A8-jit-%E7%9A%84%E6%96%B9%E6%B3%95%E7%9B%B4%E6%8E%A5%E5%9F%B7%E8%A1%8C%E4%B8%80%E5%80%8B%E5%87%BD%E6%95%B8 資工二 110910503 林成也

gakuplusq commented 2 years ago

資工二 110910530 黃劭騏

stayjay commented 2 years ago

110910507 王證傑

Selesfia commented 2 years ago

資工二 110910552 胡禎恩

wukunru commented 2 years ago

資工二110910510吳昆儒 https://github.com/wukunru/sp110b/wiki/%E7%BF%92%E9%A1%8C%E4%B8%89

jiajianong commented 2 years ago

資工二 110910535 呂嘉融

weiian000 commented 2 years ago

資工三 110813110 李維晏

derek120432 commented 2 years ago

資工二110910514李韋德

QuiLinxinag commented 2 years ago

資工二 110910536 邱麟翔

choco427 commented 2 years ago

資工二110910524張詠翔

po-hsiang666 commented 2 years ago

資工二110910544張博翔

OohelloworldoO commented 2 years ago

資工二 110910539 鄭智陽 這裡

yucing commented 2 years ago

資工二 110910511 蘇郁晴 https://github.com/yucing/sp110b/wiki/e3.md

qwezxca123 commented 2 years ago

資工二 110910533 曾旭宏

al2698 commented 2 years ago

資工二 110910531 姜呈諭

mozi5269 commented 2 years ago

資工二 110910532 王昱凱

ChiaYunn commented 2 years ago

110810535

zraiz commented 2 years ago

資工二 110911542 邵南翔 https://github.com/zraiz/sp110b/blob/master/Homework/03/Homework3.md

Kenttsai1 commented 2 years ago

資工二 110910509 蔡宗霖

daniel74859641 commented 2 years ago

110910563陳威宇 https://github.com/daniel74859641/-/wiki/hw3

WForU commented 2 years ago

資工二 110911543 何文旺 https://github.com/WForU/sp110b/wiki/HW3.md

RyanChen-01 commented 2 years ago

110910515 陳文吉 https://github.com/RyanChen-01/sp110b/wiki/hw3.md

nnnnnnn1266 commented 2 years ago

資工二110910559潘其恩

wei-annn commented 2 years ago

110910504趙唯安 https://github.com/wei-annn/sp110b/wiki/hw3.md