繁体   English   中英

C function 使用 __asm__ 命令后不返回

[英]C function doesn't return after using __asm__ command

我正在尝试使用指南https://github.com/cfenollosa/os-tutorial从头开始构建操作系统。 我已经调试了到目前为止所做的项目(第 16 章),当我尝试运行它时遇到一个问题,我发现 ip 在调用void port_byte_out(unsigned short port, unsigned char data)

  • ports.h - 调用 function 的 header 文件
unsigned char port_byte_in(unsigned short port);
void port_byte_out(unsigned short port, unsigned char data);
unsigned short port_word_in(unsigned short port);
void port_word_out(unsigned short port, unsigned short data);
  • ports.c - 被调用 function 的代码文件
    unsigned char result;
    __asm__ ( " in %%dx , %%al " : "=a" ( result ) : "d" ( port ));
    return result ;
}

void port_byte_out ( unsigned short port , unsigned char data ) {
    __asm__ ( "out %%al , %%dx " : : "a" ( data ) , "d" ( port ));
}

unsigned short port_word_in ( unsigned short port ) {
    unsigned short result ;
    __asm__ ( " in %%dx , %%ax " : "=a" ( result ) : "d" ( port ));
    return result;
}

void port_word_out ( unsigned short port , unsigned short data ) {
    __asm__ ( " out %%ax , %%dx " : : "a" ( data ) , "d" ( port ));
}
  • screen.c: set_cursor_offset(int) - 调用者代码 function
void set_cursor_offset(int offset) {
    offset /= 2;
    port_byte_out(REG_SCREEN_CTRL, 14);
    port_byte_out(REG_SCREEN_DATA, (unsigned char)(offset >> 8));
    port_byte_out(REG_SCREEN_CTRL, 15);
    port_byte_out(REG_SCREEN_DATA, (unsigned char)(offset & 0xff));
}
  • makefile - 可能是非常规使用的东西..对不起,我是新手:)
# $@ = target file
# $< = first dependcy
# $^ = all dependecies

C_SOURCES = $(wildcard kernel/*.c drivers/*.c)
HEADERS = $(wildcard kernel/*.h drivers/*.h)
OBJ = ${C_SOURCES:.c=.o}

CC = /usr/local/cross/bin/i686-elf-gcc
LD = /usr/local/cross/bin/i686-elf-ld
GDB = gdb

CFLAGS = -g

image.bin: boot_sector.bin kernel.bin
    cat $^ > $@

kernel.bin: boot/kernel_entry.o ${OBJ}
    ${LD} -o $@ -Ttext 0x1000 $^ --oformat binary

kernel.elf: boot/kernel_entry.o ${OBJ}
    ${LD} -g -o $@ -Ttext 0x1000 $^

run: image.bin 
    qemu-system-x86_64 -fda $<

debug: image.bin kernel.elf
    qemu-system-x86_64 -s -fda image.bin &
    ${GDB} -ex "target remote localhost:1234" -ex "symbol-file kernel.elf"

%.o: */%.c ${HEADERS}
    ${CC} ${FLAGS} -ffreestanding -c $< -o $@

%.o: %.asm
    nasm $< -f elf -o $@

%.bin: */%.asm
    nasm $< -f bin -o $@

clean:
    rm -rf *.bin *.dis *.o *.elf
    rm kernel/*.bin kernel/*.o boot/*.bin boot/*.o
    rm drivers/*.bin drivers/*.o
    rm image.bin
  • 文件树 -
.
├── bin
├── boot
│   ├── 32bit-gdt.asm     --> gdt table
│   ├── boot_sector.asm   --> boot sector
│   ├── disk_load.asm     --> read from disk function
│   ├── kernel_entry.asm  --> enter kernel - defines kernel_main
│   ├── print_32pm.asm    --> protected mode print(used before using the kernel functions)
│   ├── print.asm         --> real mode print
│   ├── print_hex.asm     --> real mode print hex nums
│   └── switch_32pm.asm   --> asm for switching to protected mode
├── drivers
│   ├── ports.c           --> talking to ports through the functions here
│   ├── ports.h           --> ports.c header file
│   ├── screen.c          --> screen memory writing, cursor and print functions
│   └── screen.h          --> screen.c header file
├── kernel
│   └── kernel_main.c     --> the kernel main
└── Makefile

4 directories, 14 files

我设法找到了一个解决方案,即使不遵循传统的编码方式 - 它也有效.. 我只是使用asm函数中的代码,因为它们手动内联,为什么它在我不知道并且没有任何线索之前不起作用如果有人有任何可能的想法,我想理解一个合理的解释。 感谢任何试图提供帮助的人!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM