Oxygen Basic

Programming => Problems & Solutions => Topic started by: Emil_halim on April 03, 2013, 11:24:28 AM

Title: strlen in HighLevelAsm error
Post by: Emil_halim on April 03, 2013, 11:24:28 AM
Hi all,

I have this function in High Level Asm , the problem is that it always return 0 any help please?
Code: [Select]
indexbase 0
$ filename "test4.exe"
#include "..\..\inc\RTL32.inc"

zstring a[]="ABCDE"

int H_strlen(zstring _str)
{    
  #HLAsm {
      Eax = _str
      Edx = Eax
      .while byte [Eax] != 0  
          Eax++
      .endw
      Eax -= Edx  
    }
}  

int i = H_strlen(&a)  ' always return 0
print i

BTW here is the output of high level asm
Code: [Select]
indexbase 0
$ filename "test4.exe"
#include "..\..\inc\RTL32.inc"

zstring a[]="ABCDE"

int H_strlen(zstring _str)
{

mov   Eax , _str
mov   Edx , Eax
WH__2_HW:
cmp byte [Eax] , 0

je WH__2
inc Eax
jmp WH__2_HW
WH__2:
sub  Eax , Edx
}

int i = H_strlen(&a)
print i
 
Title: Re: strlen in HighLevelAsm error
Post by: Charles Pegge on April 03, 2013, 11:58:32 AM
With high-level asm constructs in bold

zstring a[]="ABCDE"

int H_strlen(zstring*z)  'same as char*
{
 addr eax,z
 mov ecx,eax
 {
  cmp byte [eax],0
  jz exit
  inc eax
  repeat
 }
 sub eax,ecx
 return  
}

print H_strlen(a)  '5
Title: Re: strlen in HighLevelAsm error
Post by: Emil_halim on April 03, 2013, 12:19:56 PM

Charles ,

without highted  high-level asm constructs: or lowest asm level , With PLAN ASM

what is wrong with this ordinary asm code.
Code: [Select]
indexbase 0
$ filename "strlen.exe"
#include "..\..\inc\RTL32.inc"

zstring a[]="ABCDE"

int H_strlen(char* _str)
{
mov   Eax , _str
mov   Ecx , Eax
WH__2_HW:
cmp byte [Eax] , 0

je WH__2
inc Eax
jmp WH__2_HW
WH__2:
sub  Eax , Ecx
}

int i = H_strlen(&a)
print i
 
 
Title: Re: strlen in HighLevelAsm error
Post by: Charles Pegge on April 03, 2013, 12:32:33 PM
Almost. you need to use addr to resolve the char*

addr eax,_str

It is also preferable to avoid using leading/trailing underscores, since the compiler uses some of these for its own symbols, (and they hurt my eyes :))
Title: Re: strlen in HighLevelAsm error
Post by: Emil_halim on April 03, 2013, 12:40:24 PM
Quote
Almost. you need to use addr to resolve the char*

addr eax,_str

Code: [Select]
indexbase 0
$ filename "strlen.exe"
#include "..\..\inc\RTL32.inc"

zstring a[]="ABCDE"

int H_strlen(char* _str)
{
addr eax,_str
mov   Ecx , Eax
WH__2_HW:
cmp byte [Eax] , 0

je WH__2
inc Eax
jmp WH__2_HW
WH__2:
sub  Eax , Ecx
}

int i = H_strlen(&a)
print i
 

nothing changed , the same result 0 , ???
Title: Re: strlen in HighLevelAsm error
Post by: Charles Pegge on April 03, 2013, 12:57:20 PM
zstring a[]="ABCDE"

int H_strlen(char* _str)
{
addr eax,_str
mov   Ecx , Eax
WH__2_HW:
cmp byte [Eax] , 0

je WH__2
inc Eax
jmp WH__2_HW
WH__2:
sub  Eax , Ecx
return
}

int i = H_strlen(a)
print i
 
Title: Re: strlen in HighLevelAsm error
Post by: Emil_halim on April 03, 2013, 01:37:32 PM

that means the sub-Endsub is not equal to bla{  } , the first will automatically put ret instruction and the
second will not .

is this true , so why  ?
Title: Re: strlen in HighLevelAsm error
Post by: Charles Pegge on April 03, 2013, 07:11:31 PM
Part of the procedure epilog is to read a local variable  _return into the eax register. (or fpu stack, according to return type).

this will always return 0:

function f() as sys
end function


function f() as sys
   function=42 ' mov _return,42
end function

when you specify return, it skips over the _return assignment.

function f() as sys
   return 42
end function

if a return value is not specified, then the eax register is returned in its current state.

function f() as sys
  mov eax,42
   return
end function

Title: Re: strlen in HighLevelAsm error
Post by: Charles Pegge on April 03, 2013, 07:23:14 PM
For small asm functions, it is often beneficial to use macros, in terms of speed and flexibility. In some cases it produces less binary than making the equivalent procedure call.

zstring a[]="ABCDE"
sys n

macro StrLen(z,le)
{
 addr eax,z
 mov ecx,eax
 {
  cmp byte [eax],0
  jz exit
  inc eax
  repeat
 }
 sub eax,ecx
 mov le,eax
}

StrLen(a,n)
print n