I am trying to make an operating system in C. I get these errors when i compile the scripts:
gcc -m32 -nostdlib -nodefaultlibs -Wall -Wextra -ffreestanding -O2 -Iinclude -c src/io.c -o src/io.osrc/io.c:8:44: error: invalid input constraint 'a' in asm __asm__ __volatile__("outb %0, %1" : : "a"(value), "Nd"(port)); ^src/io.c:13:41: error: invalid output constraint '=a' in asm __asm__ __volatile__("inb %1, %0" : "=a"(ret) : "Nd"(port)); ^2 errors generated.make: *** [src/io.o] Error 1
This is my full code for it:
#include "io.h"#include <stdarg.h>// Function declarationvoid itoa(char* buf, int value, int base);void outb(unsigned short port, unsigned char value) { __asm__ __volatile__("outb %0, %1" : : "a"(value), "Nd"(port));}unsigned char inb(unsigned short port) { unsigned char ret; __asm__ __volatile__("inb %1, %0" : "=a"(ret) : "Nd"(port)); return ret;}void printf(const char* format, ...) { char** arg = (char**)&format; int c; char buf[20]; arg++; while ((c = *format++) != 0) { if (c != '%') { outb(0xE9, c); } else { char* p; c = *format++; switch (c) { case 'd': case 'u': case 'x': itoa(buf, *((int*)arg++), c == 'd' ? 10 : (c == 'x' ? 16 : 10)); p = buf; goto string; break; case 's': p = *arg++; if (!p) { p = "(null)"; }string: while (*p) { outb(0xE9, *p++); } break; default: outb(0xE9, '%'); outb(0xE9, c); break; } } }}void gets(char* buffer) { char* buf_ptr = buffer; while (1) { char c = inb(0x60); // Read keyboard input if (c == '\n') { *buf_ptr = '\0'; return; } else { *buf_ptr++ = c; outb(0xE9, c); // Echo character } }}// Definition of itoavoid itoa(char* buf, int value, int base) { char* ptr = buf; char* ptr1 = buf; char tmp_char; int tmp_value; if (base < 2 || base > 36) { *buf = '\0'; return; } do { tmp_value = value; value /= base; *ptr++ = "zyxwvutsrqponmlkjihgfedcba9876543210123456789abcdefghijklmnopqrstuvwxyz" [35 + (tmp_value - value * base)]; } while (value); if (tmp_value < 0 && base == 10) *ptr++ = '-'; *ptr-- = '\0'; while (ptr1 < ptr) { tmp_char = *ptr; *ptr-- = *ptr1; *ptr1++ = tmp_char; }}
I tried multiple diffrent constraints but it didn't work. I am making the kernel here and compiling it to kernel.bin
I have an makefile which runs the commands when i run "make":
C_SOURCES = $(wildcard src/*.c)OBJS = ${C_SOURCES:.c=.o}CC = gccCFLAGS = -m32 -nostdlib -nodefaultlibs -Wall -Wextra -ffreestanding -O2 -Iincludeall: kernel.binkernel.bin: $(OBJS) ld -m elf_i386 -T linker.ld -o kernel.bin $(OBJS)%.o: %.c $(CC) $(CFLAGS) -c $< -o $@clean: rm -f src/*.o kernel.bin