nasm invalid operand type

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
ITchimp
Member
Member
Posts: 137
Joined: Sat Aug 18, 2018 8:44 pm

nasm invalid operand type

Post by ITchimp »

was practising writing a boot loader,

in print, [sp+2] is causing the issue,
but move si, [bp+4] on the other hand is no problem..
what is wrong with [sp+2] expression?

Code: Select all

print:

  mov ax, [sp+2]              ; si points to start of message
  

  ;mov si, [bp+4]      ; grab the pointer to the data
  mov bh, 0x00
  mov bl, 0x00
  mov ah, 0x0E

 .loop:
  mov al, [si]
  inc si
  cmp al,0
  je return
  int 0x10
  jmp .loop


return:  

  
 

ret 2
PeterX
Member
Member
Posts: 590
Joined: Fri Nov 22, 2019 5:46 am

Re: nasm invalid operand type

Post by PeterX »

ITchimp wrote: what is wrong with [sp+2] expression?
SP is wrong.
If I remember correctly, BP, SI and DI can be used the way you do. But not SP.

Greetings
Peter
Octocontrabass
Member
Member
Posts: 6279
Joined: Mon Mar 25, 2013 7:01 pm

Re: nasm invalid operand type

Post by Octocontrabass »

ITchimp wrote:what is wrong with [sp+2] expression?
32-bit memory operands are really convenient. They work like this:

[ base + (index * scale) + displacement ]

"Base" may be EAX, ECX, EDX, EBX, ESP, EBP, ESI, EDI, or nothing.
"Index" may be EAX, ECX, EDX, EBX, EBP, ESI, EDI, or nothing (but it can't be ESP).
"Scale" may be 1, 2, 4, or 8.
"Displacement" may be any 32-bit integer, including zero.
Additionally, if "base" is ESP or EBP, the default segment is SS instead of DS.

16-bit memory operands are a lot less convenient. They work like this:

[ base + index + displacement ]

"Base" may be BX, BP, or nothing.
"Index" may be SI, DI, or nothing.
"Displacement" may be any 16-bit integer, including zero.
Additionally, if "base" is BP, the default segment is SS instead of DS.

Since SP is not allowed to be the base or index in a 16-bit memory operand, you have two choices: use a different register, or use a 32-bit memory operand.

I wouldn't recommend using a 32-bit memory operand. In real mode, an effective address that doesn't fit within 16 bits will cause an exception, so you need extra code to make a 32-bit memory operand work. Since you're writing a bootloader, you may not have space for that extra code.
ITchimp
Member
Member
Posts: 137
Joined: Sat Aug 18, 2018 8:44 pm

Re: nasm invalid operand type

Post by ITchimp »

Thanks for the reply!!! you should change your profile to God_Of_assembly!!!
Post Reply