0

I've been tasked to create a very basic text editor as a project. I'm currently trying to take an argument on the command line, open the file, read it, etc. The issue I'm having is either I'm not understanding where in memory the contents of the text file are being put, or I do understand but for some reason it's not being put there.

This is my code so far

 org 100h
 jmp start
 inHandle dw ?
 charAmount dw ?
 buff db 100 dup (?)

 start :
 xor bx, bx
 mov bl, [80h] ; length of string from command line
 cmp bl, 126 ; check length
 ja exit ; if above the length, exit
 mov [bx+81h], 0 ; add 0 to the end of the string 

 ;*** open file***
 mov ah, 3Dh ; open existing file
 mov al, 0 ; read only
 mov dx, 82h ; offset of string
 int 21h
 mov inHandle, ax ; save the handle
 jc err ; carry flag set, jump to error block 
 ;**Note i wont include the err code block, it just displays an icon on a video window to tell me it went wrong;
 jmp continue
 ;***I know this continue is probably redundant since it will go here on its own

 continue: nop 
 mov ah, 42h ; Seek end of file
 mov bx, inHandle ; Bx takes the handle
 mov al, 2 ; end of file plus offset 
 mov cx, 0 ; Upper order of bytes to move
 mov dx, 0 ; Lower order of bytes to move
 int 21h
 mov charAmount, ax ; store the length in charAmount (My file has 13 for example, so this returned 13 after seeking the end of file)

 ;*******READ FILE******
 mov ah, 3Fh ; Read file
 mov bx, inHandle ; Takes the handle
 xor cx, cx
 mov cx, charAmount ; Counter set to the length
 mov dx, offset buff ; set to buffer I defined 
 int 21h

 exit: nop ; (was used for the error code I didnt include)

 end start

So I'm confused about the read file. I'm a bit unsure what passing something called buffer to dx does. Is it an offset to give? I'm reading in documentation it says DS:DX is the pointer to read buffer. After I run my code, DS is 0700, and DX is 0112. So I look in the memory at 0700:0112 but I don't see the string from my file. It's just all 0's. Did I do something wrong? Am I forgetting something? Or am I not understanding at all where in memory this should be and I'm just looking at the wrong address. This is very frustrating and I'd appreciate the help. Thanks. I'm doing this in emu8086 by the way.

  • Have you checked for a returned error code? Have you checked that your `charAmount` is set correctly? Use a debugger or hardcode the value for testing. – Jester Dec 17 '17 at 00:56
  • Hello Jester. Thanks for the response. My CF was not set so I didn't think to check AX, a foolish mistake on my part cause from what I'm reading, AX should give the amount of bytes read if CF is not set. It says 0000 read after int21 is called. As for my charAmount, yes that is being set correct, it's value is 13 when I check the variables after running, since I have a string in the file as "Hello Pickles". – TheCoolest2 Dec 17 '17 at 01:02
  • 1
    You realize that by seeking to the end of the file you can't read anything more? :) You might want to go back to the start. – Jester Dec 17 '17 at 01:03
  • That is another thing I'm confused about. When I start from the end of the file, it returns my character amount. When I use al = 0 to start from the beginning, I get 0 for char amount. Is it because I set cx and dx wrong? – TheCoolest2 Dec 17 '17 at 01:07
  • Not sure what you are confused about... obviously offset 0 from the start is 0 bytes, and offset 0 from the end is the length of the file. It's just that you need to **move back** the file pointer. – Jester Dec 17 '17 at 01:08
  • 2
    Wow, a very dumb mistake I overlooked by forgetting to move back. Thanks! That fixed it. I guess after doing this all day my mind has melted haha. So cx and dx are offsets from wherever you have your pointer at. For example, if I have CS:DX as 00:01, Instead of "Hello.." it will be "ello.." Then 00:02 would be "llo..." etc. – TheCoolest2 Dec 17 '17 at 01:59
  • Why you use `jmp start` that does not work. instead it should be `.data` and before `start:` label there should be `.code` – Ahtisham Dec 17 '17 at 05:35
  • @Ahtisham: Why do you think that doesn't work? I think that works in a `.com` executable, and `org 100h` indicates that's what this is, IIRC. I don't think it has any advantage over putting the data after the code instead of jumping over it, but `.com` uses the tiny code model where all segment registers (including CS) have the same value. – Peter Cordes Dec 17 '17 at 11:39
  • @PeterCordes oh sorry i didn't notice this is a `.COM` executable. :) – Ahtisham Dec 17 '17 at 14:50

0 Answers0