Page 1 of 1

Command table

Posted: Thu Aug 13, 2009 1:26 am
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

Re: Command table

Posted: Thu Aug 13, 2009 1:47 am
by egos

Code: Select all

  movzx ecx,cx ; Why are you using 16-bit registers?
  call dword [call_table+ecx*4]

Re: Command table

Posted: Thu Aug 13, 2009 3:06 am
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. :(

Re: Command table

Posted: Thu Aug 13, 2009 3:25 am
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.

Re: Command table

Posted: Thu Aug 13, 2009 5:07 am
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.