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 > Segmentation fault

Author
Thread Post new topic Reply to topic
MIHIP



Joined: 14 Feb 2013
Posts: 137
Segmentation fault
Hellos!

Yesterday tried to convert pascal-like code to assembly, but it still not works. Confused
What's wrong?

Not all functions (such as read from file) implemented yet, but when i try to run it, program crashes with "Segmentation fault".

Not converted source

Code:
format ELF executable 3
entry start

segment readable executable

start:

  pushad
  mov ecx,[size]
  mov esi,[data]
  lea edi,[esi+ecx]
  xor eax,eax
  @@b:lodsb
     mov edx,eax
     mov ebx,eax
     and eax,7
     shr edx,3
     and edx,7
     shr ebx,6
     mov ah,byte[num+eax]
     mov al,byte[num+edx]
     shl eax,8
     mov al,byte[num+ebx]
     mov [edi],eax
     add edi,3
  loop @@b
  popad

segment readable writeable

num db '01234ABC',0xA
size db 1
data db 'ABC'



Pure source:


Code:
const
  wctarray[0..7of AnsiChar='ABCDEFGO';
var
  data:         array of byte;
  size:         Cardinal;
  input,outputfile;
begin
  reset  (input,paramstr(1),1);
  rewrite(output,paramstr(2),1);
  size:=filesize(input);
  SetLength(data,size shl 2+1);
  BlockRead(input,data[0],size);
  close(input);
  asm
  pushad
  mov ecx,[size]
  mov esi,[data]
  lea edi,[esi+ecx]
  xor eax,eax
  @b:lodsb
     mov edx,eax
     mov ebx,eax
     and eax,7
     shr edx,3
     and edx,7
     shr ebx,6
     mov ah,byte[wct+eax]
     mov al,byte[wct+edx]
     shl eax,8
     mov al,byte[wct+ebx]
     mov [edi],eax
     add edi,3
  loop @b
  popad
  end;
  BlockWrite(output,data[size],size*3);
  close(output);
end.



Thanks to all!
Post 08 Aug 2016, 18:26
View user's profile Send private message Visit poster's website Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 15160
Location: GW170817
You don't have any return back to the OS when the code finishes, after the POPAD is executed.
Post 08 Aug 2016, 18:43
View user's profile Send private message Visit poster's website Reply with quote
MIHIP



Joined: 14 Feb 2013
Posts: 137

revolution wrote:
You don't have any return back to the OS when the code finishes, after the POPAD is executed.



Hi!
Well, how i can realize that in linux assembly? Any examples? Confused
(binary data after popad -> file, specified in cli)
Post 08 Aug 2016, 18:52
View user's profile Send private message Visit poster's website Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 15160
Location: GW170817

MIHIP wrote:
Well, how i can realize that in linux assembly? Any examples?

Yes, of course. You need look no further than the example files included in the fasm linux download.
Post 08 Aug 2016, 19:00
View user's profile Send private message Visit poster's website Reply with quote
MIHIP



Joined: 14 Feb 2013
Posts: 137

revolution wrote:

MIHIP wrote:
Well, how i can realize that in linux assembly? Any examples?

Yes, of course. You need look no further than the example files included in the fasm linux download.



So.. I don't see any examples of calling write function in fasm examples. Huh?
Post 08 Aug 2016, 19:24
View user's profile Send private message Visit poster's website Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 15160
Location: GW170817

Code:

; fasm demonstration of writing simple ELF executable

format ELF executable 3
entry start

segment readable executable

start:

        mov     eax,4
        mov     ebx,1
        mov     ecx,msg
        mov     edx,msg_size
        int     0x80

        mov     eax,1
        xor     ebx,ebx
        int     0x80

segment readable writeable

msg db 'Hello world!',0xA
msg_size = $-msg

The final "int 0x80" with the function eax=1 is the exit back to the OS.
Post 08 Aug 2016, 19:43
View user's profile Send private message Visit poster's website Reply with quote
MIHIP



Joined: 14 Feb 2013
Posts: 137
Uhm, thanks, okay, will try to do something.
Post 08 Aug 2016, 19:45
View user's profile Send private message Visit poster's website Reply with quote
MIHIP



Joined: 14 Feb 2013
Posts: 137
So, it not works. "Segmentation fault".


Code:
format ELF executable 3
entry start

segment readable executable

start:

  pushad
  mov ecx,[size]
  mov esi,[dataz]
  lea edi,[esi+ecx]
  xor eax,eax
  @@b:lodsb
     mov edx,eax
     mov ebx,eax
     and eax,7
     shr edx,3
     and edx,7
     shr ebx,6
     mov ah,byte[num+eax]
     mov al,byte[num+edx]
     shl eax,8
     mov al,byte[num+ebx]
     mov [edi],eax
     add edi,3
  loop @@b
  popad

mov eax,4
mov ebx,1
mov ecx,[dataz]
mov edx,datasize
int 0x80

mov eax,1
xor ebx,ebx
int 0x80

segment readable writeable

num db '01234ABC'
size dd 1
dataz dd 'ABC',0xA
datasize = $-dataz

Post 08 Aug 2016, 19:55
View user's profile Send private message Visit poster's website Reply with quote
ProphetOfDoom



Joined: 08 Aug 2008
Posts: 120
Location: UK
MIHIP,
Sorry I don't have the time or the inclination to debug your code but there are two things you can do. First, run the code under gdb like so:


Code:

gdb my_program
run




When it segfaults, it will stop. Then type:

Code:

x/4i $eip




It will print the instruction that caused the segfault, followed by three more instructions. This should help you to investigate.
Another thing worth trying is to install the program "valgrind" and run your app under that:


Code:

valgrind ./my_program



It will tell you if your code is doing anything wrong like illegal reads/writes amongst other stuff.
Post 08 Aug 2016, 23:45
View user's profile Send private message Reply with quote
MIHIP



Joined: 14 Feb 2013
Posts: 137
Brilliant! Razz
Thanks Smile
Post 09 Aug 2016, 00:55
View user's profile Send private message Visit poster's website Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 15160
Location: GW170817
I suspect that mov esi,[dataz] should be mov esi,dataz.

And also that dataz dd 'ABC',0xA should be dataz db 'ABC',0xA.

There are other problems also, I'll let you figure out the rest.
Post 09 Aug 2016, 04:52
View user's profile Send private message Visit poster's website Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 15160
Location: GW170817
I thought I recognised the code:

http://board.flatassembler.net/topic.php?p=176080#176080

MIHIP: You made some very basic mistakes there when copying the code. Assembly coding requires a lot of attention to the details. And the suggestion above to use a debugger is very good. With a bit of patience you will learn a lot about what is happening by following through with the debugger.
Post 09 Aug 2016, 05:11
View user's profile Send private message Visit poster's website Reply with quote
MIHIP



Joined: 14 Feb 2013
Posts: 137
Thanks a lot, revolution! I am already has fixed code and ready to update it again. Razz
Post 09 Aug 2016, 19:22
View user's profile Send private message Visit poster's website 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-2016, Tomasz Grysztar.