0

I'm writing an operating system. My experience allows me only to make that for 32 bit, but I want to convert it to 64 bit.

What do I do to make it 64-bit?

I wanted to just know the general information without linking code but contemplated it is not related. Therefore here goes my kernel.s code:

.set MAGIC, 0x1badb002
.set FLAGS, (1<<0 | 1<<1)
.set CHECKSUM, -(MAGIC + FLAGS)

.section .multiboot
  .long MAGIC
  .long FLAGS
  .long CHECKSUM

.section .text
.extern kernelMain
.global loader

loader:
    mov $kernel_stack, %esp
    push %eax
    push %ebx
    call kernelMain

_stop:
    cli
    hlt
    jmp _stop

.section .bss
.space 2*1024*1024; # 2 megabytes of space
kernel_stack:

And I don't think it is ready yet, though.
If necessary, here is my kernel.cpp code:

#include "types.h"
#include "gdt.h"
 void print(char* str) {
    unsigned short* VideoMemory = (unsigned short*)0xb8000;   
    static uint8_t x = 0, y = 0;   
    for(int i = 0; str[i] != '\0'; ++i) {       
        switch(str[i]) {           
            case '\n':      
                y++;
                x = 0;
                break;               
            default:
                VideoMemory[80*y+x] = (VideoMemory[80*y+x] & 0xFF00) | str[i];
                x++;
                break;
        }
        if(x >= 80) {
            y++;
            x = 0;
        }
        if (y >= 25) {
            for(y = 0; y < 25; y++)
                for(x = 0; x < 80; x++)
                    VideoMemory[80*y+x] = (VideoMemory[80*y+x] & 0xFF00) | ' ';
            x = 0;
            y = 0;
        }
    }
}
extern "C" void kernelMain(void* multiboot_structure, unsigned int magicnumber) {
    print("All the includes work fine, but I will only link them if really necessary!\n");   
    GlobalDescriptorTable gdt;
    while(1);
}



I am building my assembly code with the as assembler in Ubuntu; my C++ code by g++.

If there are duplicates, please link them because I didn't discover them even though scrolled through a whole page.

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
LeopardL GD
  • 139
  • 13
  • 2
    @KViiTEN: `bits 32` is NASM syntax, not GAS. And no, it's using 32-bit operand-size for `push %eax`, which wouldn't assemble in 64-bit mode. Also, x86-64 System V passes args in RDI, RSI, RDX, RCX, R8, R9 before falling back to using the stack. – Peter Cordes Jun 13 '20 at 17:47
  • Do you know how to make it 64bit though? @Peter Cordes – LeopardL GD Jun 13 '20 at 17:51
  • I don't know how to change the multiboot header to indicate that the bootloader should jump to it already in 64 instead of 32-bit mode. The calling convention changes for calling C++ from asm are as I described, though; args in regs. Look at compiler output for C++ code that calls functions. – Peter Cordes Jun 13 '20 at 17:59
  • 1
    That header is "multiboot version 1", which doesn't support 64-bit. This means that you either have to change to "multiboot version 2" (and rewrite your header and any "disk image generator" scripts) or write 32-bit code in assembly to switch to long mode/64-bit (and fight the linker to make it work). – Brendan Jun 13 '20 at 18:07
  • I still don't get it. Can anyone create a discussion? I'm kinda new to the assembly language – LeopardL GD Jun 13 '20 at 18:15
  • @PeterCordes Please read upper – LeopardL GD Jun 13 '20 at 22:49
  • @LeopardLGD: Probably the best option would be to switch to "multiboot 2", which supports 64-bit itself (and avoids you needing to learn how to do CPU mode switches, etc), and also supports UEFI properly (which will make your code more future proof, with the negligible disadvantage of not being compatible with old/deprecated versions of GRUB and the increased complexity of GRUB2) – Brendan Jun 14 '20 at 06:09
  • @LeopardLGD: In any case; you'll want to figure out how you'll precede (mutli-boot2, your own code to switch to 64-bit using multi-boot1, something else) and research (and maybe ask a new question/s) your choice. – Brendan Jun 14 '20 at 06:11
  • 1
    I. Have. No. Idea. How. To. Do. It. Can you start a chat/discussion and explain? I am really a beginner in Assembly... (this is my main account of LeopardL) – KViiTEN Jun 14 '20 at 10:55
  • +, KViiTEN is me – LeopardL GD Jun 14 '20 at 14:24
  • 1
    @KViiTEN: The GRUB people provide example OS code (using multiboot 2) that might help get you started: https://www.gnu.org/software/grub/manual/multiboot2/html_node/Example-OS-code.html – Brendan Jun 16 '20 at 07:27
  • Thanks, gonna check – LeopardL GD Jun 16 '20 at 07:58
  • Is that actually 32? Isn't it 16-bit real mode? Where is gdt/pm setup? – vp_arth Oct 10 '20 at 14:17
  • i don't just have this file. i have lots of others - interrupts.s, gdt.cpp, gdt.h, and lots more. I want to know about the assembly code - how to make it 64-bit? – LeopardL GD Oct 11 '20 at 15:16

0 Answers0