1 Replies - 1890 Views - Last Post: 10 October 2012 - 03:38 PM

#1 supratroopa  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 08-October 12

File not reading in x86 assembly (NASM on DOS)

Posted 10 October 2012 - 10:14 AM

So I'm writing a program in assembly that gets a file name from the user, opens the file, reads it, and prints the content to the screen. My goal is to eventually have it count the number of words in the file, but right now I just want it to read the content to verify that I'm doing all this correctly.

Here's my code (it's a lot, I know). I have not implemented the function for printing the screen yet:

[GLOBAL mystart]

[EXTERN _printf]
[EXTERN _atoi]

;define constants for buffer size and interupt
%define DOS 0f1h
%define INBUFFSIZE 40
%define FILEBUFFSIZE 4096

;define macro for printing on screen
%macro printLn 1
    mov ah, 09h
    mov edx, %1
    int DOS

[SECTION .text]


    ;prints "Enter the filename: " on screen
    printLn string

    ;takes filename from user
    mov ah, 0Ah
    mov byte [infile], INBUFFSIZE
    mov byte [infile+1], 0
    mov edx, infile
    int DOS

    ;pads filename string with 0
    movzx ecx, byte [infile+1]
    mov byte [infile+ecx+2], 0

    ;opens the file (read mode)
    mov ah, 3Dh
    mov al, 0
    mov edx, infile+2
    int DOS
    mov [inhandle], ax
    jc failureJump

    ;reads the file
    mov ah, 3Fh
    mov bx, [inhandle]
    mov edx, filebuff
    mov cx, FILEBUFFSIZE
    int DOS
    jc failureJump
    printLn string3
    mov [nbytes], ax

    ;nbytes stores the number of bytes in the file, so that we can loop through it 
    ;prepare for looping through characters
    mov ecx, 0
    mov cl, [nbytes]
    mov ebx, 0

    ; I want to see how many bytes are in the file
    printLn ecx

    printLn string4
    jmp endProcess
    printLn string2


[SECTION .data]
string db       "Enter a filename: ", 13, 10, '$'
string2 db      "Found a dot!", 13, 10, '$'
string3 db      "First break!", 13, 10, '$'
string4 db      "Second break!", 13, 10, '$'
string5 db      "Third break!", 13, 10, '$'

[SECTION .bss]
infile: resb 512
filebuff: resb FILEBUFFSIZE
inhandle: resw 1
nbytes: resw 1
nbytes2: resw 1 

So as you can see, I have a couple break-strings and jc failureJumps in the code just to see where it's getting to. As it stands, the code does not work. Upon execution, it asks for the filename, but fails after first break, with a long error "Exiting due to signal SIGSEGV", among other things. It also mentions that the ecx register has the value ffffffff. If I comment out the "printLn ecx" line, the program seems to work (it gets to "Second break!"), though sluggishly, and I have no hard evidence that it is reading the file correctly (I do have a file for this: myfile.txt). Does anyone know what I'm doing wrong? Thanks in advance!

Is This A Good Question/Topic? 0
  • +

Replies To: File not reading in x86 assembly (NASM on DOS)

#2 GunnerInc  Icon User is offline

  • "Hurry up and wait"
  • member icon

Reputation: 917
  • View blog
  • Posts: 2,358
  • Joined: 28-March 11

Re: File not reading in x86 assembly (NASM on DOS)

Posted 10 October 2012 - 03:38 PM

Why don't you just use the interrupt number instead of DOS? In all my years I have never seen that done, it is just a wide open door for bugs, and it is a nightmare for others reading your code.

There is no such Interupt on DOS - 0f1h to read a file. Most Interrupts to open a file on DOS are int 21h
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1