flat assembler
Message board for the users of flat assembler.

flat assembler > Linux > API Macros

Goto page 1, 2  Next

┬┐Useless?
Yes
30%
 30%  [ 4 ]
No
69%
 69%  [ 9 ]
Total Votes : 13

Author
Thread Post new topic Reply to topic
Thaorius



Joined: 27 Jul 2006
Posts: 42
Hi everyone.

I'm new to assembler programming.I just started a few days ago.

I just started reading about linux assembly programming, so, i don't like interruption system, i prefer the windows style, just call the function. So, an idea come's to my mind:
Why don't make an api wrapper library? for example:

Code:
proc sys_write fd, buffer, buffer_len
mov eax,SYS_WRITE
mov ebx,fd
mov ecx,buffer
mov edx,buffer_len
int 80h
endp    


This way is (for me at least) with just write this i get the api called:
Code:
invoke sys_write,1,_msg,_msg_size    


What do you think about it? like I sayd before, I'm new to assembly programming and fasm as well, so I'm afraid that this is useless and that is better to write directly the interruptions.

P/D: I'm from Argentina and my english is not really good, somebody speak spanish around here?
P/D2: If i write this, did you like that i post it here?

Thanks for your opinnion
Post 31 Jul 2006, 04:01
View user's profile Send private message Visit poster's website MSN Messenger Reply with quote
vid
Verbosity in development


Joined: 05 Sep 2003
Posts: 7109
Location: Slovakia
you can use libc as such "wrapper library". btw, i am (in theory Wink ) doing something like this in FASMLIB.

unfortunately i haven't been working on it since lately.

If you would stick to few FASMLIB standards we could merge it
Post 31 Jul 2006, 11:34
View user's profile Send private message Visit poster's website AIM Address MSN Messenger ICQ Number Reply with quote
Thaorius



Joined: 27 Jul 2006
Posts: 42
I know about libc but is slow and make my program to big.

About FASMLIB, is the same project that i read in a post of 4 pages? Are you guys stil doing it? there is some version available for downlaod?

What are the standards?

Thanks
Post 31 Jul 2006, 13:58
View user's profile Send private message Visit poster's website MSN Messenger Reply with quote
vid
Verbosity in development


Joined: 05 Sep 2003
Posts: 7109
Location: Slovakia
well, only i was doing it, plus i used few routines from other people here and only I am involved now.

About the standards, i can give you "official" ones, but you will best see it from sources.

For now, look at my API wrapper for win32 file access. Would you like to write wrapper for linux with same interface? This could also push me a little to move this project further.

standards - doc/rules.txt
file access wrapper - fasmlib/win32/file.inc

don't care about error handling for now, just return with CF=1 on error, i found that global variable is NOT a solution for returning error, it will have to be changed
Post 31 Jul 2006, 14:56
View user's profile Send private message Visit poster's website AIM Address MSN Messenger ICQ Number Reply with quote
vid
Verbosity in development


Joined: 05 Sep 2003
Posts: 7109
Location: Slovakia
oh, and FASMLIB thread is here:

http://board.flatassembler.net/topic.php?t=4696
Post 31 Jul 2006, 15:18
View user's profile Send private message Visit poster's website AIM Address MSN Messenger ICQ Number Reply with quote
vid
Verbosity in development


Joined: 05 Sep 2003
Posts: 7109
Location: Slovakia
if you are going to write such library, then it would be shame if everyone would be writing his own library and FASM will remain without lib. So if you dislike something from standard you are welcomed to discuss
Post 31 Jul 2006, 15:25
View user's profile Send private message Visit poster's website AIM Address MSN Messenger ICQ Number Reply with quote
Thaorius



Joined: 27 Jul 2006
Posts: 42
I'm looking up the library right now. I will try to write the api wrapper, but how do i add it? in linux, in a file called api.inc? or how?

And about standars, like i'm new to assembly i didn't have any preferences yet so the actual one's are fine.

Bye
Post 31 Jul 2006, 16:46
View user's profile Send private message Visit poster's website MSN Messenger Reply with quote
vid
Verbosity in development


Joined: 05 Sep 2003
Posts: 7109
Location: Slovakia
i spent a lot of time creating the standards, currently they are best part of FASMLIB Wink
(well, except error handling)

please paste me some working example of linux executable, then i will prepare FASMLIB for you so you can easily add these wrappers.
Post 31 Jul 2006, 17:21
View user's profile Send private message Visit poster's website AIM Address MSN Messenger ICQ Number Reply with quote
okasvi



Joined: 18 Aug 2005
Posts: 383
Location: Finland
I think, that wrapper around linux system calls should be made to be macro-package.

And wouldnt it be 'stdcall api,...'?

vid, now that I'm setting up again my slackb0x in near future, I will be looking into FASMLIB, and who knows, I might have something to contribute to it Smile
Post 31 Jul 2006, 17:50
View user's profile Send private message MSN Messenger Reply with quote
vid
Verbosity in development


Joined: 05 Sep 2003
Posts: 7109
Location: Slovakia
okasvi: thanks, some feedback always boosts me to work on it.

why macros? i think procs are better. macros probably wouldn't preserve registers, or if yes, then it would be too big overhead.

just for fun, here is reading file into allocated buffer in FASMLIB
Code:
libcall file.open, "abcd.txt", 0
jc error
mov ebx, eax

libcall file.seek, 2, 0
jc error
mov ecx, eax

libcall mem.alloc, ecx
jc error
mov edi, eax

libcall file.read, ebx, edi, ecx
jc error

libcall file.close, ebx
jc error
    
Post 31 Jul 2006, 18:00
View user's profile Send private message Visit poster's website AIM Address MSN Messenger ICQ Number Reply with quote
okasvi



Joined: 18 Aug 2005
Posts: 383
Location: Finland
OIC, never gave thought about preserving registers... Now it makes more sense to make them as proc's for me too Smile
And debugging is easier when you clearly see that it's calling something etc.


edit: I just voted, but did read question wrongly, at first I thought I saw 'usefull?' Razz
Post 31 Jul 2006, 18:05
View user's profile Send private message MSN Messenger Reply with quote
Thaorius



Joined: 27 Jul 2006
Posts: 42
I started writing this:
Code:
; GNU/Linux API Wrapper

macro sys_exit  .code{
        mov eax, 1              ; Select sys_exit function
        mov ebx, .code          ; Exit code
        int 80h                 ; Call the kernel
}

proc sys_exit .code
        mov eax, 1              ; Select sys_exit function
        mov ebx, .code          ; Exit code
        int 80h                 ; Call the kernel
        ret                     ; Return
endp    

Is the easiet api, but, a problem is coming, the procedure way causes an error in fasm:
Code:
Thaorius:/home/thaorius/asm# fasm linapi.asm
flat assembler  version 1.67.6  (16384 kilobytes memory)
fasmlib/linux/api.inc [10]:
proc sys_exit .code
error: illegal instruction.
Thaorius:/home/thaorius/asm#       


In win32 i can use procedures, but why the error on linux?

About the linux example:
Code:
format ELF executable
entry start

include "fasmlib/linux/api.inc"

segment readable executable

start:
        sys_exit 1 ; Macro way
        ;invoke sys_exit,1 ; procedure way, doesn't work...

segment readable writeable

_msg db 'hello world',10,0 ; data section    


Bye!
Post 31 Jul 2006, 19:07
View user's profile Send private message Visit poster's website MSN Messenger Reply with quote
Thaorius



Joined: 27 Jul 2006
Posts: 42
Wow, it was a macro inclusion issue.

Now it let me use procedures but it gives me an error and onestly i don't know how to solve it :S
Code:
format ELF executable
entry start

include "fasmlib/linux/api.inc"

segment readable executable

start:
        invoke sys_exit,1    


And the error:
Quote:
Thaorius:/home/thaorius/asm# fasm linapi.asm
flat assembler version 1.67.6 (16384 kilobytes memory)
linapi.inc [9]:
invoke sys_exit,1
fasmlib/linux/proc32.inc [20] invoke [6]:
call [proc] }
error: operand size not specified.
Thaorius:/home/thaorius/asm#


Any ideas?

Thanks
Post 31 Jul 2006, 19:55
View user's profile Send private message Visit poster's website MSN Messenger Reply with quote
UCM



Joined: 25 Feb 2005
Posts: 285
Location: Canada
It is not invoke, it is stdcall.
Invoke calls an indirect procedure, like a Win32 API call.
Stdcall calls a direct procedure, like one defined with proc.
Post 31 Jul 2006, 20:24
View user's profile Send private message Reply with quote
Thaorius



Joined: 27 Jul 2006
Posts: 42
Thanks
Post 31 Jul 2006, 20:38
View user's profile Send private message Visit poster's website MSN Messenger Reply with quote
vid
Verbosity in development


Joined: 05 Sep 2003
Posts: 7109
Location: Slovakia
thanks, i will prepare FASMLIB version as soon as i can.

Code:
;invoke a, b, c
push c
push b
call [a]

;stdcall a, b, c
push c
push b
call a    
Post 01 Aug 2006, 07:30
View user's profile Send private message Visit poster's website AIM Address MSN Messenger ICQ Number Reply with quote
vid
Verbosity in development


Joined: 05 Sep 2003
Posts: 7109
Location: Slovakia
okay, here is FASMLIB template for linux. It's compilable, but i couldn't test it.

right now it supports only "exit" procedure, located in "fasmlib/linux/process.inc". I didn't have any linux kernel API reference to support others.

For beginning, you could try to implement "mem" module. Create file "mem.inc" in fasmlib/linux/, and write there 2 procedures "mem.alloc" and "mem.free", in style of "exit" procedure.

Then change main file to:
Code:
format ELF executable
entry start

;FASMLIB doesn't yet have implemented it's own "proc" macros
;so we need to include this one, it's from standard windows macropackage
include "proc32.inc"

;------------------------------------------------------------
; CODE
segment readable executable

SYSTEM equ linux
include "fasmlib/fasmlib.inc"
include "fasmlib/process.inc"
include "fasmlib/mem.inc"

start:
        libcall mem.alloc, 12345
        jc      error

        libcall mem.free, eax
        jc      error

        libcall exit, 0

error:
        libcall exit, 1

;------------------------------------------------------------
; DATA
segment readable writeable
        IncludeIData
        IncludeUData    


please post what you have (mem.alloc and mem.free) even if they don't work, i will help you to make them work in FASMLIB style. thanks for help with lib

and don't forget to check if the template (with only "exit") works, before making any changes to it.


Description:
Download
Filename: fasmlib_linuxtemplate.zip
Filesize: 33.17 KB
Downloaded: 111 Time(s)

Post 02 Aug 2006, 12:17
View user's profile Send private message Visit poster's website AIM Address MSN Messenger ICQ Number Reply with quote
Thaorius



Joined: 27 Jul 2006
Posts: 42
Apparently to allocate memory on linux i should call sys_brk a couple of times, but there is a problem with that, the function have a *parameter, so is a memory pointer, in the procedure, how do i use mov instruction to put a memory address inteast of the contents of the local variable?

or should i use lea?

Thanks
Post 02 Aug 2006, 17:05
View user's profile Send private message Visit poster's website MSN Messenger Reply with quote
Thaorius



Joined: 27 Jul 2006
Posts: 42
Oh, i wrote this:
Code:
; Linux memory routines

proc mem.alloc edseg, memv
        stdcall sys_brk, 0              ; Call to sys_brk
        
        test eax,eax                    ; Check for errors
        
        js .error               ; Jump on error
        
        add     eax, [edseg]    ; Allocate memory
        stdcall sys_brk, eax
        js .error               ; Jump on error
        
        cmp eax,[memv+1]        ; Check the segment
        js .error               ; Jump on error
        
        clc                     ; Clear carry flag
        jmp .ret                ; Jump to return instruction
.error:
        stc                     ; Set carry flag
        jmp .ret                ; Jump to return instruction
.ret:
        ret                     ; Return
endp 

proc mem.free memv
        stdcall sys_brk, [memv] ; Deallocation
        
        test eax,eax            ; Check for error
        js .error               ; If the sign flag is set go to error
        
        clc                     ; Clear carry flag
        jmp .ret                ; Jump to return instruction
.error:
        stc                     ; Set carry flag
        jmp .ret                ; Jump to return instruction
.ret:
        ret                     ; Return
endp    


Based on this link: http://www.faqs.org/docs/Linux-HOWTO/Assembly-HOWTO.html#AEN1005

Anyway, it pretend to do it, but i'm not sure, it test it with this:
Code:
format ELF executable
entry start

;------------------------------------------------------------
; CODE
segment readable executable

SYSTEM equ linux
include "fasmlib/linux/api.inc"
include "fasmlib/fasmlib.inc"
include "fasmlib/process.inc" ;for "exit" procedure
include "fasmlib/mem.inc"

start:
        libcall mem.alloc, 4000000h
        jc .error
        
        mov     ebx,eax                 ; save bottom
        mov     eax,memvar              ; eax=beginning of data segment

;.cycle:        
;       mov     word    [eax],1         ; fill up with 1's
;       inc     eax
;       cmp     ebx,eax                 ; current pos = bottom?
;       jne     .cycle
        
        stdcall mem.free, memvar
        jc .error
        
        jmp .exit
.error:
        stdcall sys_write, 1, _msg, _msg_size
        jmp .exit
.exit:
        stdcall sys_write, 1, _msg2, _msg2_size
        stdcall sys_exit, 1
        

;------------------------------------------------------------
; DATA
segment readable writeable
        _msg db "Ooooppss...",10,0
        _msg_size = $-_msg
        _msg2 db "You're a lucky guy!",10,0
        _msg2_size = $-_msg2
        IncludeIData
        IncludeUData
        memvar db 1    

If you uncomment this lines:
Code:
;.cycle:        
;       mov     word    [eax],1         ; fill up with 1's
;       inc     eax
;       cmp     ebx,eax                 ; current pos = bottom?
;       jne     .cycle    

You get a segment violation error, on runtime.

But if you don't... the program end with no error.

And this is a draft of the api wrapper:
Code:
; GNU/Linux API Wrapper
include 'macros.inc'

proc sys_exit stdcall uses eax ebx,ecode
        mov eax, 1              ; Select sys_exit function
        mov ebx, [ecode]        ; Exit code
        int 80h                 ; Call the kernel
        ret                     ; Return
endp

proc sys_fork stdcall
        mov eax, 2              ; Select sys_fork function
        int 80h                 ; Call the kernel
        
        test eax, eax           ; Check for error
        js .error               ; Jump on error
        
        clc                     ; Clear carry flag
        jmp .ret                ; Jump to return instruction
.error:
        stc                     ; Set carry flag
        jmp .ret                ; Jump to return instruction
.ret:
        ret                     ; Return
endp

; TEST THIS FUNCTION, IT READS BUT RETURNS ERROR ANYWAY
proc sys_read stdcall uses eax ebx ecx edx, fd, buffer, nbytes
        mov eax, 3              ; Select sys_read function
        mov ebx, [fd]           ; File Descriptor
        mov ecx, [buffer]       ; Buffer address
        mov edx, [nbytes]       ; Bytes to read
        int 80h                 ; Call the kernel
        
        test eax,eax            ; Check for error
        js .error               ; If the sign flag is set go to error
        
        clc                     ; Clear carry flag
        jmp .ret                ; Jump to return instruction
.error:
        stc                     ; Set carry flag
        jmp .ret                ; Jump to return instruction
.ret:
        ret                     ; Return
endp

proc sys_write stdcall uses ebx ecx edx, fd, buffer, buffer_len
        mov eax, 4              ; Select sys_write function
        mov ebx, [fd]           ; File Descriptor
        mov ecx, [buffer]       ; Buffer
        mov edx, [buffer_len]   ; Buffer Len
        int 80h                 ; Call the kernel
        
        test eax,eax            ; Check for error
        js .error               ; If the sign flag is set go to error
        
        clc                     ; Clear carry flag
        jmp .ret                ; Jump to return instruction
.error:
        stc                     ; Set carry flag
        jmp .ret                ; Jump to return instruction
.ret:
        ret                     ; Return
endp

proc sys_creat stdcall uses ebx ecx, what, mode
        mov eax, 8              ; Select sys_creat function
        mov ebx, [what]         ; File to create
        mov ecx, [mode]         ; Mode
        int 80h                 ; Call the kernel
        
        test eax,eax            ; Check the file descriptor
        js .error               ; If the sign flag is set go to error
        
        clc                     ; Clear carry flag
        jmp .ret                ; Jump to return instruction
.error:
        stc                     ; Set carry flag
        jmp .ret                ; Jump to return instruction
.ret:
        ret                     ; Return
endp

proc sys_brk stdcall uses ebx, edsegment
        mov eax, 45             ; Select sys_brk function
        lea ebx, [edsegment]    ; End data segment
        int 80h                 ; Call the kernel
        
        test eax,eax            ; Check for error
        js .error               ; If the sign flag is set go to error
        
        clc                     ; Clear carry flag
        jmp .ret                ; Jump to return instruction
.error:
        stc                     ; Set carry flag
        jmp .ret                ; Jump to return instruction
.ret:
        ret                     ; Return
endp    


So, ideas are welcome.

Bye!
Post 02 Aug 2006, 17:53
View user's profile Send private message Visit poster's website MSN Messenger Reply with quote
vid
Verbosity in development


Joined: 05 Sep 2003
Posts: 7109
Location: Slovakia
Thaorius: i thinked memory allocation is just simple call in linux... seems it will be little too complicated for beginning (reallocations etc.). Let's start with file I/O instead.

Create file "fasmlib/linux/file.inc" and implement there "file.open" and "file.close" for beginning. Look at "fasmlib/win32/file.inc" also.

PS: nice list: http://www.lxhp.in-berlin.de/lhpsyscal.html

btw: does the version which only calls "exit" work, or does it crash?
Post 03 Aug 2006, 07:10
View user's profile Send private message Visit poster's website AIM Address MSN Messenger ICQ Number Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  
Goto page 1, 2  Next

< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2018, Tomasz Grysztar.

Powered by rwasa.