I am trying to make a OS and am writing the print function in 32 bit protected mode nasm. Here is my code:
mov edx, 1
mov ebx, 0xb8000 160*edx
inc edx
However when I run this I get the following error:
kernel.asm:23: error: invalid operand type
Why is this error happening and how do I fix it?
CodePudding user response:
Thing is that your instruction is invalid, because the SIB byte of the instruction encoding on x86 ISA only allows scale factors of 2
,4
and 8
.
But with mov ebx, 0xb8000 160*edx
you tried to scale EDX
by 160. And this is not a valid scale factor. You have to calculate the value with other instructions like MUL
and SHL
or INC
to get the resulting value you want.
For example, you can do the following calculation to position your cursor (with EDX=Y):
mov eax, 160 ; set decimal value 160
mul edx ; EAX:EDX = eax*edx (EAX= y coord)
add eax,0B8000h ; Add value to lower 32-bits of the result
mov ebx, eax ; EBX = EAX
add ebx, x ; add x index
add ebx, x ; Now EBX is Y*160 X*2
The order of the instructions can be changed, but you should get the gist of it.