Question

So, today I tried to create a library for my in-development OS with one simple function: To print characters on screen. To use this function I simply have to push the string address to the stack and call it (The string must end with a 0x00 byte). Below is the source code of the function:

__print:                        ;Print string that is terminated with a 0x00 to screen

__print_prepare:
    pop si                  ;SI Register = String Address
    mov ah, 0x0E                ;0x0E Function = Print Character on screen
    mov bx, 0x0F                ;Background = Black, CharColor = White
    xor cx, cx              ;Counter = 0

__print_check:
    push byte [cx + si]         ;Push character to the stack
    or byte [cx + si], byte [cx + si]   ;Check if the byte equals 0x00
    jz __print_exit             ;If true then exit
    jmp __print_main            ;Else print the character

__print_main:
    pop al                  ;Store the byte in the AL Register
    int 0x10                ;Call interupt 0x10 = BIOS Interupt
    jmp __print_next            ;Continue to next character

__print_next:
    inc cx                  ;Increment CX Register by one for next character = Counter
    jmp __print_check           ;Check authenticity of character

__print_exit:
    ret
    

Anytime I try to assemble the source code the following nasm error appears:

def_os_lib.asm:10: error: invalid combination of opcode and operands

def_os_lib.asm:11: error: invalid combination of opcode and operands

def_os_lib.asm:16: error: invalid combination of opcode and operands

Also, in some cases, that is when I compile it in ELF format, it prompts this error:

def_os_lib.asm:10: error: impossible combination of address sizes

def_os_lib.asm:11: error: impossible combination of address sizes

The command I use for nasm(bin) is:

nasm -f bin def_os_lib.asm

The command I use for nasm(elf64) is:

nasm -f elf64 def_os_lib.asm

I've just started assembly and I know I'm doing a too big step for my knowledge. I just want to go a little bit more in depth.

Thank you all for your help. I have completed the source code by correcting the mistakes with your suggestions. Here is the new code:

__print:                        ;Print string that is terminated with a 0x00 to screen

__print_prepare:
    pop si                  ;SI Register = String Address
    xor bx, bx              ;Counter = 0

__print_check:
    push bx                 ;Save BX
    xor ax, ax              ;AX = 0
    add bx, si              ;BX = Address of the character
    mov al, byte [bx]           ;AL = Character
    pop bx                  ;Restore BX
    push ax                 ;Save character
    or ax, ax               ;Check if the byte equals 0x00
    jz __print_exit             ;If true then exit
    jmp __print_main            ;Else print the character

__print_main:
    pop ax                  ;Store the byte in the AL Register
    push bx                 ;Save BX
    mov bx, 0x0F                ;Background = Black, CharColor = White
    mov ah, 0x0E                ;0x0E Function = Print Character on screen
    int 0x10                ;Call interupt 0x10 = BIOS Interupt
    jmp __print_next            ;Continue to next character

__print_next:
    pop bx                  ;Restore BX
    inc bx                  ;Increment CX Register by one for next character = Counter
    jmp __print_check           ;Check authenticity of character

__print_exit:
    ret
    
Was it helpful?

Solution 2

You can only use [BX or BP] in combination with [SI or DI]; AX, DX or CX are not allowed.

You can't PUSH/POP a byte so POP AL so any byte-size argument will be deemed invalid.

OTHER TIPS

push byte [cx + si]

cx cannot be used in adressing under real mode.

or byte [cx + si], byte [cx + si]

You can not have two memory operands in one instruction normally in x86. You need to use a register as middleman.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top