flat assembler
Message board for the users of flat assembler.
 Home   FAQ   Search   Register 
 Profile   Log in to check your private messages   Log in 
flat assembler > Linux > Attempt at implementing cat in fasm.

Author
Thread Post new topic Reply to topic
alkap



Joined: 18 Feb 2015
Posts: 40
Location: Dnipro, Ukraine

Attempt at implementing cat in fasm.

I am attempting to implement cat in fasm. It is still in the early stages. The current implementation expects a single file passed to it on command line, and catenates the file. I am also trying to get my head round the different calling conventions. The current implementation wraps the system calls into functions, and passes parameters to them via the stack. However, the last character written to STDOUT is ETX (i.e. end-of-text, ascii value 03h). I cannot seem to see what is doing that. Passing parameters to the syscalls in registers does not generate ETX. It is probably something simple I am not seeing. Any tips would be much appreciated.

Code:

Post 25 Apr 2015, 14:39
View user's profile Send private message Send e-mail Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 15324
Location: Bigweld Industries

The code you posted also has ETX at the end. Perhaps your editor/viewer software is faulty?
Post 25 Apr 2015, 14:55
View user's profile Send private message Visit poster's website Reply with quote
alkap



Joined: 18 Feb 2015
Posts: 40
Location: Dnipro, Ukraine

Sorry. That was intentional. Just wanted to post the output generated verbatim.
Post 25 Apr 2015, 16:21
View user's profile Send private message Send e-mail Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 15324
Location: Bigweld Industries

It occurs to me that ETX is the same as CTRL-C. Does the Linux stdin send a CTRL-C code to indicate the end of a text stream?
Post 26 Apr 2015, 00:22
View user's profile Send private message Visit poster's website Reply with quote
alkap



Joined: 18 Feb 2015
Posts: 40
Location: Dnipro, Ukraine

Mystery solved.
Turns out, I used the wrong base pointer offset when accessing the number-of-bytes parameter in my implementation of the 'read' and 'write' functions:

Code:

read:
        push ebp
        mov     ebpesp

        mov     eaxREAD
        mov     ebx, [ebp+08h]
        mov     ecx, [ebp+0Ch]
        mov     edx, [ebp+0Eh]    ; wrong offset; should read '[ebp+10h]
        int     80h

        mov     espebp
        pop     ebp
        ret




Code:

write:
        push    ebp
        mov     ebpesp

        mov     eaxWRITE
        mov     ebx, [ebp+08h]
        mov     ecx, [ebp+0Ch]
        mov     edx, [ebp+0Eh]    ; wrong offset; should read '[ebp+10h]'
        int     80h

        mov     espebp
        pop     ebp
        ret




Thanks for trying to help.
Post 27 Apr 2015, 14:29
View user's profile Send private message Send e-mail Reply with quote
alkap



Joined: 18 Feb 2015
Posts: 40
Location: Dnipro, Ukraine

Here is a version of 'cat' that uses 'cdecl' calling convention.
I also intend to implement a version of the program that passes parameters to functions using registers.
Constructive criticism would be appreciated.

Code:


flib_stack.inc

Code:

; int open(const char *pathname, int flags)
align 16
open:
        push    ebp
        mov     ebpesp

        mov     eaxOPEN
        mov     ebx, [ebp+08h]
        mov     ecx, [ebp+0Ch]
        int     80h

        mov     espebp
        pop     ebp
        ret

; int close(int fd)
align 16
close:
        push    ebp
        mov     ebpesp

        mov     eaxCLOSE
        mov     ebx, [ebp+08h]
        int     80h

        mov     espebp
        pop     ebp
        ret

; ssize_t read(int fd, void *buf, size_t count)
align 16
read:
        push    ebp
        mov     ebpesp

        mov     eaxREAD
        mov     ebx, [ebp+08h]
        mov     ecx, [ebp+0Ch]
        mov     edx, [ebp+10h]
        int     80h

        mov     espebp
        pop     ebp
        ret

; ssize_t write(int fd, const void *buf, size_t count)
align   16
write:
        push    ebp
        mov     ebpesp

        mov     eaxWRITE
        mov     ebx, [ebp+08h]
        mov     ecx, [ebp+0Ch]
        mov     edx, [ebp+10h]
        int     80h

        mov     espebp
        pop     ebp
        ret

; void _exit(int status)
align 16
exit:
        push    ebp
        mov     ebpesp

        mov     eaxEXIT
        mov     ebx, [ebp+08h]
        int     80h

        mov     espebp
        pop     ebp
        ret

; syscalls
EXIT                    = 1
READ            = 3
WRITE           = 4
OPEN            = 5
CLOSE           = 6


Post 04 May 2015, 10:46
View user's profile Send private message Send e-mail Reply with quote
alkap



Joined: 18 Feb 2015
Posts: 40
Location: Dnipro, Ukraine

Here's a version of 'cat' that passes function parameters via registers.
Constructive criticism, and/or suggestions on a better/simpler implementation would be much appreciated.

Code:


flib_regs.inc

Code:

; int open(const char *pathname, int flags)
; eax - syscall
; ebx - pathname
; ecx - flags
; eax - file descriptor
align 16
open:
        mov     eaxOPEN
        int     80h
        ret

; int close(int fd)
; eax - syscall
; ebx - file descriptor
align 16
close:
        mov eaxCLOSE
        int 80h
        ret

; ssize_t read(int fd, void *buf, size_t count)
; eax - syscall
; ebx - file descriptor
; ecx - buffer
; edx - size of buffer
; eax - nbytes read into buffer
align 16
read:
        mov     eaxREAD
        int     80h
        ret

; ssize_t write(int fd, const void *buf, size_t count)
; eax - syscall
; ebx - file desciptor
; ecx - buffer
; edx - bytes to be written
align 16
write:
        mov     eaxWRITE
        int     80h
        ret

; void _exit(int status)
; eax - syscall
; ebx - status
align 16
exit:
        mov     eaxEXIT
        int     80h

; syscalls
EXIT                    = 1
READ            = 3
WRITE           = 4
OPEN            = 5
CLOSE           = 6


Post 12 May 2015, 18:45
View user's profile Send private message Send e-mail Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< 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


Powered by phpBB © 2001-2005 phpBB Group.

Main index   Download   Documentation   Examples   Message board
Copyright © 2004-2017, Tomasz Grysztar.