Author Topic: open any file  (Read 5536 times)

Offline Medussa

  • Jr. Member
  • *
  • Posts: 2
open any file
« on: November 11, 2013, 10:44:10 PM »
Hey!! im trying to open a file(.txt)  and show what is inside in screen  but i dont know what im doing wrong, if anyone knows please help me:




Code: [Select]
sys_exit        equ     1
sys_read        equ     3
sys_write       equ     4
sys_open equ 5
stdin           equ     0
stdout          equ     1


section .bss
bufLen equ 100
buffer resb bufLen

fileBufLen equ 250
fileBuf resb fileBufLen

lenresp equ 40
bufresp resb  lenresp

section .data ; data section

msgpre: db "name of file: "
lenpre: equ $-msgpre

section .text

global _start

_start:

mov ecx,msgpre
mov edx, lenpre
call DisplayText
; read from keyboard

mov ecx,bufresp
mov edx,lenresp
call ReadText



mov ebx,bufresp
mov ecx, 0
mov eax,sys_open
int 80h


mov ebx, eax
mov ecx, fileBuf
mov edx, fileBufLen
mov eax, sys_read
int 80h

mov ecx,fileBuf
mov edx,fileBufLen
call DisplayText


   

fin: 
    mov     eax, sys_exit
    xor     ebx, ebx
    int     80H

   
   

DisplayText:
    mov     eax, sys_write
    mov     ebx, stdout
    int     80H
    ret

ReadText:
    mov     ebx, stdin
    mov     eax, sys_read
    int     80H
    ret

[/font]




else i want to know a syscall where i can remove a file, if anyone knows that too please help me
« Last Edit: November 11, 2013, 11:33:26 PM by Frank Kotler »

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
Re: open any file
« Reply #1 on: November 12, 2013, 12:52:14 AM »
Well, you need a zero-terminated string for the filename to open a file, and sys_read does not give you a zero-terminated string. Your input is LF-terminated (10 or 0xA), number of bytes read - including the LF - is in eax, so it's fairly easy to zero-terminate. (if the pesky user enters more than you've allowed in edx, the input is NOT terminated with LF - for now, just don't) This seems to fix it.

By rights, you ought to check for error after any system call (and perhaps other places). In my experience, sys_open is fairly likely to fail. I've showed you a "trick" to display the error number by typing "echo $?" after the file exits. As long as you don't typo the filename, you may not need this, but it's a mistake to try to continue after a failed call!

Code: [Select]
sys_exit        equ     1
sys_read        equ     3
sys_write       equ     4
sys_open equ 5
stdin           equ     0
stdout          equ     1


section .bss
bufLen equ 100
buffer resb bufLen

fileBufLen equ 250
fileBuf resb fileBufLen

lenresp equ 40
bufresp resb  lenresp

section .data ; data section

msgpre: db "name of file: "
lenpre: equ $-msgpre

section .text

global _start

_start:

mov ecx,msgpre
mov edx, lenpre
call DisplayText
; read from keyboard

mov ecx,bufresp
mov edx,lenresp
call ReadText
mov byte [ecx + eax - 1],0 ; zero-terminate the string


mov ebx,bufresp
mov ecx, 0
mov eax,sys_open
int 80h
cmp eax, -4096 ; did we succeed?
ja fin

mov ebx, eax
mov ecx, fileBuf
mov edx, fileBufLen
mov eax, sys_read
int 80h

mov ecx,fileBuf
mov edx,fileBufLen
call DisplayText

    xor eax, eax ; claim "no error"
fin: 
    mov ebx, eax ; error number (if any) in ebx
    neg ebx ; for easy readability
    mov     eax, sys_exit
;    xor     ebx, ebx
    int     80H

   
   

DisplayText:
    mov     eax, sys_write
    mov     ebx, stdout
    int     80H
    ret

ReadText:
    mov     ebx, stdin
    mov     eax, sys_read
    int     80H
    ret

This could/should be improved. You display 250 bytes, regardless of how much you read from the file. You probably want to save length actually read, and display just that. If there's more than 250 bytes in the file, you may want to loop back and read a buffer-full, display a buffer-full... until done. It's a start.

sys_unlink is 10. Be careful with it!

Best,
Frank