flat assembler
Message board for the users of flat assembler.

Index > Linux > Attempt at implementing cat in fasm.

Author
Thread Post new topic Reply to topic
alkap



Joined: 18 Feb 2015
Posts: 44
Location: Dnipro, Ukraine
alkap 25 Apr 2015, 14:39
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: 20302
Location: In your JS exploiting you and your system
revolution 25 Apr 2015, 14:55
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: 44
Location: Dnipro, Ukraine
alkap 25 Apr 2015, 16:21
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: 20302
Location: In your JS exploiting you and your system
revolution 26 Apr 2015, 00:22
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: 44
Location: Dnipro, Ukraine
alkap 27 Apr 2015, 14:29
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     ebp, esp

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

        mov     esp, ebp
        pop     ebp
        ret
    

Code:
write:
        push    ebp
        mov     ebp, esp

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

        mov     esp, ebp
        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: 44
Location: Dnipro, Ukraine
alkap 04 May 2015, 10:46
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     ebp, esp

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

        mov     esp, ebp
        pop     ebp
        ret

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

        mov     eax, CLOSE
        mov     ebx, [ebp+08h]
        int     80h

        mov     esp, ebp
        pop     ebp
        ret

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

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

        mov     esp, ebp
        pop     ebp
        ret

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

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

        mov     esp, ebp
        pop     ebp
        ret

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

        mov     eax, EXIT
        mov     ebx, [ebp+08h]
        int     80h

        mov     esp, ebp
        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: 44
Location: Dnipro, Ukraine
alkap 12 May 2015, 18:45
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     eax, OPEN
        int     80h
        ret

; int close(int fd)
; eax - syscall
; ebx - file descriptor
align 16
close:
        mov eax, CLOSE
        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     eax, READ
        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     eax, WRITE
        int     80h
        ret

; void _exit(int status)
; eax - syscall
; ebx - status
align 16
exit:
        mov     eax, EXIT
        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


Copyright © 1999-2024, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.