简体   繁体   English

在一个简单的 32 位内核中混合 C 和汇编代码

[英]Mixing C and assembly code in a simple 32 bit kernel

I have a simple implementation of a 32 bit kernel that I have written in C (it's a freestanding implementation): (The VGA screen is in 80*25 color text mode)我有一个用 C 编写的 32 位内核的简单实现(它是一个独立的实现):(VGA 屏幕处于 80*25 彩色文本模式)

#include<stdint.h>

#define COLOR 0x39
#define VIDEO_MEMORY 0xb8000

uint32_t get_cursor();
void set_cursor(uint32_t);

void puts(char* Message);
void kmain()
{
        puts("This is a test message..");
        return;
}

void puts(char* Message)
{
        uint32_t pointer = get_cursor();
        pointer <<= 1;
        char* VGA_CURSOR = (char*) VIDEO_MEMORY;
        VGA_CURSOR += pointer;
        while   (*Message)
        {
                *VGA_CURSOR = *Message;
                Message++;
                VGA_CURSOR ++;
                *VGA_CURSOR = COLOR;
                VGA_CURSOR ++;
        }
        VGA_CURSOR -= VIDEO_MEMORY;
        pointer = (uint32_t)VGA_CURSOR;
        pointer >>= 1;
        set_cursor(pointer);
}

The set_cursor and get_cursor functions are defined in assembly as such: set_cursor 和 get_cursor 函数在程序集中定义如下:

[bits 32]
[global set_cursor]
[global get_cursor]

set_cursor:   ;ebx is the location

        mov ebx,[esp+0x18]
        mov al, 0x0f ;Refer to the index register table port mapping for CRT (low byte)
        mov dx, 0x3d4 ; port number CRT index
        out dx,al   ;Write 0x0f in port 0x3D4  --- note that the port registers are 1 byte in size
        mov dx,0x3d5 ;port number CRT data
        mov al,bl
        out dx,al

        mov al, 0x0e ;Refer to the index register table port mapping for CRT (high byte)
        mov dx, 0x3d4 ; port number CRT index
        out dx,al
        mov dx,0x3d5 ;port number CRT data
        mov al,bh
        out dx,al

        ret

get_cursor:
        mov al, 0x0f ;Refer to the index register table port mapping for CRT (low byte)
        mov dx, 0x3d4 ; port number CRT index
        out dx,al   ;Write 0x0f in port 0x3D4  --- note that the port registers are 1 byte in size
        mov dx,0x3d5 ;port number CRT data
        in al,dx   ;Store the low byte in al -- Hardware forced to use al
        mov bl,al

        mov al, 0x0e ;Refer to the index register table port mapping for CRT (high byte)
        mov dx, 0x3d4 ; port number CRT index
        out dx,al   ;Write 0x0f in port 0x3D4  --- note that the port registers are 1 byte in size
        mov dx,0x3d5 ;port number CRT data
        in al,dx   ;Store the high byte in al -- Hardware forced to use al
        mov bh,al   ;Store the high byte in bh

        xor eax,eax
        mov ax,bx

        ret

Here, the getcursor function seems to work fine (It gets the value of the cursor that ranges from 0 to 80*25) I know that the implementation of setcursor is right too.在这里,getcursor 函数似乎工作正常(它获取范围从 0 到 80*25 的游标值)我知道 setcursor 的实现也是正确的。 (I've used it in pure assembly codes). (我在纯汇编代码中使用过它)。 I believe that there is an error in passing of the values on the stack.我相信在堆栈上传递值时存在错误。 Where could I have been wrong?我哪里错了?

Looks like the place the error existed was in the function parameter passing.看起来错误存在的地方是在函数参数传递中。 An objdump of the c code shows this in the place where the function is called: c 代码的 objdump 在调用函数的地方显示了这一点:

  73:   ff 75 f0                pushl  -0x10(%ebp)
  76:   e8 fc ff ff ff          call   77 <printf+0x5e>
  7b:   83 c4 10                add    $0x10,%esp

[ebp-0x10] is my pointer variable... The place where I got confused was probably the add esp,0x10 . [ebp-0x10] 是我的指针变量......我感到困惑的地方可能是add esp,0x10 Even though I'd passed only 1 parameter (of 4 bytes), it seemed to keep 16 bytes on the stack?即使我只传递了 1 个参数(4 个字节),它似乎在堆栈上保留了16 个字节?

However, looks like [esp] was the return address (4 bytes = 32 bits), [esp + 4] is the first parameter.... Maybe the 16 bytes were for proper alignment但是,看起来 [esp] 是返回地址(4 个字节 = 32 位),[esp + 4] 是第一个参数......也许 16 个字节是为了正确对齐

Thanks to Michael Petch: https://stackoverflow.com/users/3857942/michael-petch and Peter Cordes: https://stackoverflow.com/users/224132/peter-cordes for their comments!感谢 Michael Petch: https : //stackoverflow.com/users/3857942/michael-petch和 Peter Cordes: https ://stackoverflow.com/users/224132/peter-cordes 的评论!

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

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