Author Topic: file : how to use lseek in nasm in linux  (Read 6642 times)

Offline jang

  • New Member
  • Posts: 1
file : how to use lseek in nasm in linux
« on: March 19, 2015, 10:35:59 AM »
hai,

I wish to append a file at the end of file.
but this is not working.

please help me 

Code: [Select]

%macro mymac 4
mov eax,%1
mov ebx,%2
mov ecx,%3
mov edx,%4
int 80h
%endmacro

section .data
msg1 db 'enter filename  : '
ml1 equ $-msg1
msg2 db 'file contents are   : ',10
ml2 equ $-msg2
msg3 db 'File not exists !!! ',10
ml3 equ $-msg3
info db 'hello'
l equ $-info
;fname db 'abc.txt',0

section .bss
fname resb 50
flen resb 1
buff resb 20
bsize resb 2
fd resb 1

section .text
global _start

_start:
mymac 4,1,msg1,ml1

mymac 3,0,fname,50
mov [flen],eax
dec eax
mov byte [fname+eax],0

mymac 5,fname,2,0777
mov [fd], byte eax

cmp eax,0
jg proceed
mymac 4,1,msg3,ml3
jmp exit

proceed:

mymac 3,[fd],buff,20
dec eax
mov [bsize], byte eax

mymac 19,[fd],0,2

mymac 4,[fd],info,l

mymac 6,[fd],0,0

mymac 4,1,msg2,ml2
mymac 4,1,buff,bsize

exit: mymac 1,0,0,0


Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
Re: file : how to use lseek in nasm in linux
« Reply #1 on: March 19, 2015, 04:09:15 PM »
Hi jang,

Thanks for joining us!

How does it "not work"? Help us help you!

Well... lemme look at it. Hmmm, the warnings might give you some clue - some of your uninitialized variables need to be "resd" not "resb". 0777 probably needs to be octal. Next to last line - address of bsize?

There may be more... Give me some help, here!

Best,
Frank


Offline Zefferon

  • Jr. Member
  • *
  • Posts: 13
Re: file : how to use lseek in nasm in linux
« Reply #2 on: March 30, 2015, 10:16:55 PM »
Welcome to the band.

I would suggest using seperate macros for each function call.
Your general purpose macro will have a developer looking up system call function numbers until he/she memorizes them.
Specific macros are much easier to read and understand.

Example:
OPEN A FILE:
%macro   FILE_OPEN 2-4 2,600q
   MOV EAX, 5
   MOV EBX, %1   ;addr of nullstring of filename
   MOV ECX, %3  ;mode
   MOV EDX, %4  ;permissions
   INT 80h
   MOV %2, EAX   ;save handle returned in EAX
   BT   EAX, 31    ;set carry if error
%endmacro

FILE_OPEN MyFile, [MyFileHandle]
FILE_OPEN MyFile, [MyFileHandle], O_RDONLY
FILE_OPEN MyFile, [MyFileHandle], O_RDWR, PERMISSION_OWNER_ALL

OPEN A FILE FOR APPENDING:
%macro   FILE_OPEN_APPEND 2-3 600q
   MOV EAX, 5
   MOV EBX, %1        ;addr of nullstring of filename
   MOV ECX, 2001q   ;O_WRONLY + O_APPEND
   MOV EDX, %3
   INT 80h
   MOV %2, EAX   ;save handle returned in EAX
   BT   EAX, 31    ;set carry if error
%endmacro

FILE_OPEN_APPEND MyFile, [MyFileHandle]

The above examples have defaults for the most common usages of the system calls.
Use defines for your constants.
Appending to a file is a mode option under Linux.

Here are some examples to help you build your macro library:
http://www.shallizar.com/nasmcollection/Linux/Shallizar/Runtime/StringLib/Prtz.mac
http://www.shallizar.com/nasmcollection/Linux/Shallizar/Runtime/StringLib/PrtLong.mac
http://www.shallizar.com/nasmcollection/Linux/Shallizar/Runtime/FileSystem/FileSystem.mac