CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
+ Reply to Thread
Results 1 to 2 of 2
  1. #1
    Join Date
    Apr 2012
    Posts
    1

    How to read text file, line by line using NASM

    Hi all,

    I'm new to this forum and new using Assembly language.

    If possible, I would like to know how can I read from a text file, line by line, ignoring the "/n" and knowing when the file ends.

    The main idea is to have a program that can find the right unciphered phrase against cipheredtext.txt file using a dictionary (dic.txt), I already have a macro that do that job! My problem is to know how to read the words and pass that "word" to the macro.

    Thanks.

  2. #2
    Join Date
    Feb 2012
    Location
    Fremont,CA
    Posts
    28

    Re: How to read text file, line by line using NASM

    section .bss
    buffer: resb 2048 ; A 2 KB byte buffer used for read

    section .data
    buflen: dw 2048 ; Size of our buffer to be used for read

    section .text
    global _start
    _start:
    ; open(char *path, int flags, mode_t mode);

    ; Get our command line arguments.
    pop ebx ; argc
    pop ebx ; argv[0] (executable name)
    pop ebx ; argv[1] (desired file name)
    mov eax, 0x05 ; syscall number for open
    xor ecx, ecx ; O_RDONLY = 0
    xor edx, edx ; Mode is ignored when O_CREAT isn't specified
    int 0x80 ; Call the kernel
    test eax, eax ; Check the output of open()
    jns file_read ; If the sign flag is set (positive) we can begin reading the file

    ; = If the output is negative, then open failed. So we should exit
    exit:
    mov eax, 0x01 ; 0x01 = syscall for exit
    xor ebx, ebx ; makes ebx technically set to zero
    int 0x80

    ; = Begin reading the file

    file_read:
    ; read(int fd, void *buf, size_t count);
    mov ebx, eax ; Move our file descriptor into ebx
    mov eax, 0x03 ; syscall for read = 3
    mov ecx, buffer ; Our 2kb byte buffer
    mov edx, buflen ; The size of our buffer
    int 0x80
    test eax, eax ; Check for errors / EOF
    jz file_out ; If EOF, then write our buffer out.
    js exit ; If read failed, we exit.
    ; Didn't read the whole file, so just output what we got and be done with it.
    ; ^ This is blah and needs to be updated when I find out how

    file_out:
    ; write(int fd, void *buf, size_t count);
    mov edx, eax ; read returns amount of bytes read
    mov eax, 0x04 ; syscall write = 4
    mov ebx, 0x01 ; STDOUT = 1
    mov ecx, buffer ; Move our buffer into the arguments
    int 0x80
    jmp exit ; All done
    Thanks and Regards By Cygnis Media

+ Reply to Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts



HTML5 Development Center

Click Here to Expand Forum to Full Width