0

i want to call lseek and llseek syscall (without using glibc wrapper, i'm doing this for learning purpose) from asm. i wrote the code below to do just that, but when i run it, it shows segmentation fault which i understand is happening because i'm trying access a memory out of the block allocated to us. Any way i can solve it?

section .text
   global _start         ;must be declared for using gcc

_start:  

   ;open the file for reading
   mov eax, 5
   mov ebx, file_name
   mov ecx, 2             ;for RDWD access
   mov edx, 0777          ;read, write and execute by all
   int  0x80

   mov  [fd_out], eax

   ; write into the file
   mov  edx,len          ;number of bytes
   mov  ecx, msg         ;message to write
   mov  ebx, [fd_out]    ;file descriptor 
   mov  eax,4            ;system call number (sys_write)
   int  0x80             ;call kernel

   ;change offset
   mov eax, 19
   mov ebx, [fd_out]
   mov ecx, 7             ;for read only access
   mov edx, 0             ;SEEK_SET
   int  0x80

   ; write into the file
   mov  edx,lntwo          ;number of bytes
   mov  ecx, mgafter         ;message to write
   mov  ebx, [fd_out]    ;file descriptor 
   mov  eax,4            ;system call number (sys_write)
   int  0x80             ;call kernel


section .data
file_name db 'myfile.txt'
msg db 'Welcome to jj'
mgafter db ' Shashwat'
lntwo equ $-mgafter
len equ  $-msg

section .bss
fd_out resb 1
fd_in  resb 1
info resb  26
Shashwat shagun
  • 121
  • 1
  • 6
  • 2
    You segfault because execution continues after the last `sys_write`, but you don't have any code there. So whatever is next in memory is decoded as instructions (which fault). Use a debugger. Also run your program under `strace`. See the bottom of the x86 tag wiki for debugging tips https://stackoverflow.com/tags/x86/info – Peter Cordes Feb 26 '18 at 23:34
  • Added the dup target to the x86 tag wiki; if anyone knows a better duplicate for falling off the end of functions (or `_start` specifically), please update the tag wiki. – Peter Cordes Feb 26 '18 at 23:44

0 Answers0