2020301918-os/user/hack.c
2022-11-04 13:57:40 +08:00

38 lines
1.1 KiB
C

#include <assert.h>
#include <user/stdio.h>
#include <user/syscall.h>
#define PID_SNAKE 0
#define PAGE_SIZE 4096
#define DATA_SEG_SIZE (4 * PAGE_SIZE)
typedef struct Food {
char hint[16];
int x;
int y;
int score;
} Food;
char __attribute__((aligned(0x1000))) snake_seg_data[DATA_SEG_SIZE];
// this attribute is necessary to make the pointer 4K aligned
int main()
{
Food* food = NULL;
lml_mmap(PID_SNAKE, (void*)0x0804d000, (void*)snake_seg_data, DATA_SEG_SIZE);
// data segment:
// Type Offset VirtAddr PhysAddr FileSiz MemSiz Flg Align
// LOAD 0x004000 0x0804d000 0x0804d000 0x0163c 0x12500 RW 0x1000
// 'This is a food' starts @ file offset 0x542c, while .data starts @ 0x4000
// so, offset is 0x142c
food = (Food*)(snake_seg_data + 0x142c);
// printf("%s\n", food->hint);
while (1) {
// printf("%d %d", food->x, food->y);
if (food->x <= 0) food->x = 1;
if (food->y <= 0) food->y = 1;
if (food->x >= 70) food->x = 69;
if (food->y >= 25) food->y = 24;
// according to some trial, XMAX(right veritcal line border)=69, YMAX(low edge of screen)=24
}
return 0;
}