Command table

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
sema4
Posts: 5
Joined: Tue Aug 11, 2009 9:30 pm

Command table

Post by sema4 »

I am trying to add a command table to my os. I can compare the data that was typed into the prompt with the table and determine which command was entered, but I can figure out how to make it call the function.

Here is the command table:

Code: Select all

commands:
db 'hi', 0
db 'help', 0
db '?', 0
db 0xFF		; end of command table

call_table:
dd cmd_hi
dd cmd_help
dd cmd_help
Here is the code to compare the command with the command table:

Code: Select all

	mov	si, commands	;set si to command table
	mov	di, buffer	;set di to command
	mov cx, 0
	jmp NewChar

NextCmd:
	inc cx
	mov	di, buffer	;set di to command

NewChar:
	lodsb					; AL = [SI]
	test	al, al			; if [si] = 0 then end of command 
	jz	FoundCmd			; in command table
	
	mov	ah, [di]			; load [di]
	inc	di					; next char in command
	or	ah, 0x20			; convert user input to lowercase
	cmp	al, ah				; compare user to command table
	jz	NewChar
		
LoopTo0:					; ok not this command 
	lodsb					; so now we need to go the end
	test al, al				; of command in the command table
	jnz	LoopTo0				; marked by 0

	cmp byte [si], 0xFF
	jne	NextCmd				; no? - good, check next command

	mov si, badcommand
	call print

	jmp mainloop

FoundCmd:
	;How do i call the function here?
	jmp	mainloop
egos
Member
Member
Posts: 612
Joined: Fri Nov 16, 2007 1:59 pm

Re: Command table

Post by egos »

Code: Select all

  movzx ecx,cx ; Why are you using 16-bit registers?
  call dword [call_table+ecx*4]
If you have seen bad English in my words, tell me what's wrong, please.
sema4
Posts: 5
Joined: Tue Aug 11, 2009 9:30 pm

Re: Command table

Post by sema4 »

Could you explain to me why I should be using 32-bit registers in this case? I don't know much about asm. :(
User avatar
kop99
Member
Member
Posts: 120
Joined: Fri May 15, 2009 2:58 am

Re: Command table

Post by kop99 »

Could you explain to me why I should be using 32-bit registers in this case? I don't know much about asm. :(
You should better read the IA 2, 3.
And there are tons of asm books.
User avatar
gravaera
Member
Member
Posts: 737
Joined: Tue Jun 02, 2009 4:35 pm
Location: Supporting the cause: Use \tabs to indent code. NOT \x20 spaces.

Re: Command table

Post by gravaera »

I'm not extraordinarily versed in ASM myself, and I usually only use it where absolutely needed. If I were you, I'd make the call_table an array of function pointers.

Code: Select all

call_table:
   _cmd1 dd 0
   _cmd2 dd 0
   _cmd3 dd 0

cmd1:
   mov eax, 0
ret

cmd2:
   mov eax, 1
ret

cmd3:
   mov eax, 2
ret

setup_cmd_table:
   mov ebx, cmd1
   mov _cmd1, ebx
   mov ebx, cmd2
   mov _cmd2, ebx
   mov ebx, cmd3
   mov _cmd3, ebx
ret

; In order to call any of the commands, you just access the index in cmd_table, and do like so:
   mov ebx, cmd1
   call [ebx]
But I'm sure someone more versed (*coughDexcough*) could probably offer something more solid.
17:56 < sortie> Paging is called paging because you need to draw it on pages in your notebook to succeed at it.
Post Reply