BITS 16

start:
	mov ax, 07C0h		; Set up 4K stack space after this bootloader
	add ax, 288		; (4096 + 512) / 16 bytes per paragraph
	mov ss, ax
	mov sp, 4096

	mov ax, 07C0h		; Set data segment to where we're loaded
	mov ds, ax


	mov si, text_string	; Put string position into SI
	call print_string	; Call our string-printing routine

	


	text_string db 'This is my cool new OS!', 0


print_string:			; Routine: output string in SI to screen
	mov ah, 0Eh		; int 10h 'print char' function

.repeat:
	loadsb			; Get character from string
	cmp al, 0
	je .background_colors		; If char is zero, end of string
	int 10h			; Otherwise, print it
	jmp .repeat
	
.background_colors:
	mov ah,0Bh
	mov bh,0
	mov bl,02h
	int 10h
	jmp .cursor_pointer
	
.cursor_pointer:
	mov ah,2h		;set the value to "ah" to move the cursor pointer
	mov bh,0		;select page 
	mov dh,22		;set row position of the cursor
	mov dl,0		;set column position of the cursor
	int 10h			;tell bios "hey dude now we are done,take action!!!"
	jmp .character_write

.character_write:
	mov ah,9h
	mov al,65		;ascii value
	mov bh,0		;display page
	mov bl,5h		;display attribute
	mov cx,10		;display times
	int 10h
	jmp .cursor_pointer2

.cursor_pointer2:
	mov ah,2h		;set the value to "ah" to move the cursor pointer
	mov bh,0		;select page 
	mov dh,23		;set row position of the cursor
	mov dl,0		;set column position of the cursor
	int 10h			;tell bios "hey dude now we are done,take action!!!"
	jmp .character_read
	
.character_read:
	mov ah,00h		;remeber set the value to "ah" now you are going to read a keyboard input
	int 16h			;this interrupt is given to control keystrokes of keyboard
	jmp .print_input	;now lets print what we took as input

.print_input:
	mov ah,9h		;set the value to "ah" to print one character to std out
	mov bh,0		;set page number
	mov bl,5h		;set the color attribute of the font to be print
	mov cx,10		;set the value how many time the character in "al" should be print
	int 10h			;tell bios "hey dude now we are done,take action!!!"
	jmp .done
	
.done:
	ret

	times 510-($-$$) db 0	; fill up the rest part of the boot secotr with 0 s
	dw 0xAA55		; The standard PC boot signature

I'm writing a simple bootloader to get a key board input. but it gets some errors. i'm using QEMU as my emulator.please help me to find those errors

Dani AI

Generated

— nice start; is right about the assembler typo: use lodsb (not loadsb). Fixing that will clear the immediate assemble error, but there are a couple of runtime/logic problems that will keep QEMU from showing the expected behavior.

Main problems and how to fix them

  • Return into data: you call the print routine and then immediately define the string. The ret in the routine will pop the return address (the address of the data) and start executing bytes of the message as code. Move the message before the code, or place a short jump to skip the data (common pattern: jmp start / message: db ... / start:).
  • Make labels mean what you expect: add org 0x7C00 at the top (recommended) so label addresses match where BIOS loads the sector, or make sure your segment setup matches the addressing scheme you chose — pick one approach and be consistent.
  • End condition: a plain ret is not a good program endpoint for a boot sector unless you return to valid code. After finishing work, use a safe halt (e.g. cli/hlt) or an infinite loop (jmp $) or chain to the next stage (int 19h) depending on your goal.
  • Keyboard/print flow: int 0x16 (AH=0) returns the ASCII in AL; int 0x10 teletype (AH=0x0E) prints AL. If you use int 0x10 AH=9 you must set AL, BL and CX correctly for repeated writes. Verify the registers used by each BIOS call.

Minimal layout example (safe ordering)

org 0x7C00
jmp start

msg: db 'Boot OK',0

start:
  ; setup segments/stack here
  mov si, msg
  call print_msg
  cli
  hlt

print_msg:
  mov ah,0x0E
.loop:
  lodsb
  cmp al,0
  je .done
  int 0x10
  jmp .loop
.done:
  ret

Quick test/checklist

  • Assemble to a 512-byte binary (nasm -f bin boot.asm -o boot.bin).
  • Confirm the last two bytes are 0x55 0xAA and run in QEMU (for example qemu-system-i386 -fda boot.bin).
  • If it still fails, single-step with QEMU/GDB or add small visual markers (repeated characters/colors) to find where execution lands.

Line 26: loadsb should be lodsb

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.