diff --git a/Makefile b/Makefile index 7264c5b..460ee24 100644 --- a/Makefile +++ b/Makefile @@ -97,7 +97,7 @@ $(OBJ_DIR)/%.o: $(SRC_DIR)/%.s $(AS) $(ASFLAGS) -o $@ $< $(OBJCOPY) --remove-section .note.gnu.property $@ -MBR_TEST_OBJ = obj/mbr_test.o obj/serial.o +MBR_TEST_OBJ = obj/mbr_test.o obj/serial.o obj/itoa.o mbr_test: $(MBR_TEST_OBJ) $(DISK) $(LD) -T src/mbr_test.ld -o $(BIN_DIR)/mbr_test.bin $(MBR_TEST_OBJ) diff --git a/src/itoa.s b/src/itoa.s new file mode 100644 index 0000000..857079b --- /dev/null +++ b/src/itoa.s @@ -0,0 +1,45 @@ +.section .text +.code16 + +.globl itoa + +.equ ITOA_BUFFER_SIZE, 12 + +# eax - number +# ecx - base +itoa: + push %bx + mov $numbers, %bx + +.debug: + jmp .debug + + mov $(itoa_result + ITOA_BUFFER_SIZE - 1), %di + std +.div: + divl %ecx + or %eax, %eax + jz .end # we run out of numbers + + # yep, we're using LUT for number -> character conversion since ASCII is a fuck + push %eax + add %bx, %ax + stosb + pop %eax + + jmp .div +.end: + cld + + # we return ptr to string, since we're pushing chars in reverse, therefore + # beginning of string will change + mov %di, %ax + + pop %bx + ret + +.section .bss +.comm itoa_result, ITOA_BUFFER_SIZE + +.section .rodata +numbers: .ascii "0123456789ABCDEF" \ No newline at end of file diff --git a/src/mbr_test.s b/src/mbr_test.s index 29e90f0..0cb9b2f 100644 --- a/src/mbr_test.s +++ b/src/mbr_test.s @@ -29,11 +29,22 @@ mov %ax, %ss xor %sp, %sp + # set ES segment + push %ds + pop %es + .entry: call serial_init mov $msg, %si call prints + + mov $12345678, %eax + mov $10, %ecx + call itoa + + mov %ax, %si + call prints .halt: jmp .halt