flat assembler
Message board for the users of flat assembler.

Index > Main > Program too large for memory

Goto page 1, 2  Next
Author
Thread Post new topic Reply to topic
sbeng



Joined: 11 Oct 2012
Posts: 12
sbeng 11 Oct 2012, 14:30
Hi,
i'm a newbie about fasm. I'm coding a simple asm file to read a file (txt or binary). When i execute com file, it does show "Program too large for memory".
There my code:


Code:
org 100h
start:
READ_ROM:
;read file and save content in memory array
mov AH,3Dh
mov AL,0; readfile  
mov DX,File_CH16
int 21h
mov [HANDLE16],AX
jc ERR_READ_ROM
mov dx,OK
mov ah,09h
int 21h
jz CLOSE_ROM
ret;

ERR_READ_ROM:
        mov dx,Error_String
        mov ah,09h
        int 21h
        ret
CLOSE_ROM:
        mov AH,3EH
        mov BX,HANDLE16
        int 21H
;AH=3E
        ret;
PRINT_STATUS_OK:
ret;
PRINT_STATUS_KO:
ret
HANDLE16  DW 0
File_CH16 db 'C:/path/hello.txt$'
Error_String DB 'Errore$'
OK DB 'OK$'
    
Post 11 Oct 2012, 14:30
View user's profile Send private message Reply with quote
Picnic



Joined: 05 May 2007
Posts: 1389
Location: Piraeus, Greece
Picnic 11 Oct 2012, 15:06
Hi,

Try with a null-terminated string.

Code:
File_CH16 db 'C:/fasm/hello.txt',0  
    
Post 11 Oct 2012, 15:06
View user's profile Send private message Visit poster's website Reply with quote
sbeng



Joined: 11 Oct 2012
Posts: 12
sbeng 12 Oct 2012, 17:57
Ok now i'm writing an exe program that need to read a simple txt file, open it and print OK or ERROR message.
When i execute it (on a Windows Vista dos window) it does appears below message: "example.COM The NTVDM CPU has encountered an illegal istrction". My code is correct?
Best regards.


Code:
format MZ
READ_ROM:

;read file and save content in memory array
mov AH,3Dh
mov AL,0; readfile
mov DX,File_CH16
int 21h
mov [HANDLE16],AX

jc PRINT_STATUS_KO
jmp PRINT_STATUS_OK
jmp CLOSE_ROM
JMP END_PROCESS

CLOSE_ROM:
        mov AH,3EH
        mov BX,HANDLE16
        int 21H
PRINT_STATUS_OK:
        mov dx,OK
        mov ah,09h
        int 21h
PRINT_STATUS_KO:
        mov dx,Error_String
        mov ah,09h
        int 21h
END_PROCESS:

HANDLE16  DW 0
File_CH16 db 'C:\download\test\ciao.txt',0
Error_String db 'Errore=$',0
OK db 'OK$',0
    
Post 12 Oct 2012, 17:57
View user's profile Send private message Reply with quote
Picnic



Joined: 05 May 2007
Posts: 1389
Location: Piraeus, Greece
Picnic 12 Oct 2012, 21:19
Some quick observations,

END_PROCESS needs some exit code, usually it's
Code:
mov ax, 004Ch
int 21h
    


jump to PRINT_STATUS_OK will display also PRINT_STATUS_KO -it probably will crash since it's missing the exit routine-

'jmp CLOSE_ROM' and 'JMP END_PROCESS' never be executed.

[HANDLE16] needs brackets at CLOSE_ROM subroutine.


It may be easier if you split code into small routines.
Here is a simple example, originally posted by rCX.

Code:
        include "c:\fasm\include\macro\if.inc"

        MAX_FILE_SIZE = 1024

        org 100h

        call open_file
        .if ( CARRY? )
            mov dx, error_open
            call print_text
            jmp exit
        .endif

        mov [handle], ax

        call read_file
        .if ( CARRY? )
            mov dx, error_read
            call print_text
            call close_file
            jmp exit
        .endif

        mov di, ax
        add di, filedata
        mov al, '$'
        cld
        stosb

        mov dx, filedata
        call print_text
        call close_file

exit:
        mov ax, 4c00h
        int 21h

print_text:
        push ax
        mov ah, 09h
        int 21h
        pop ax
        ret

open_file:
        push dx
        mov ah, 3Dh
        mov al, 00h
        mov dx, filename
        int 21h
        pop dx
        ret

read_file:
        push bx cx dx
        mov bx, [handle]
        mov ah, 3Fh
        mov cx, MAX_FILE_SIZE
        mov dx, filedata
        int 21h
        pop dx cx bx
        ret

close_file:
        push bx
        mov ah, 3Eh
        mov bx, [handle]
        int 21h
        pop bx
        ret

handle dw ?
filename db "C:\download\test\ciao.txt",0
error_open db "File not found.$"
error_read db "Can't read file.$"
filedata rb MAX_FILE_SIZE+1 
    


if you like to output null-terminated strings you have to write your own print_text routine.
Here is a very simple one, written on the fly.
Code:
print_text:
        push ax cx
        mov cx, MAX_FILE_SIZE
        mov ah, 0Eh
        cld
@@:     lodsb
        test al, al
        jz @F
        int 10h
        loop @B
@@:     pop cx ax
        ret
    


In order to use it, dx must be replaced by si, these are the changes that must be made.
Code:
        ;mov dx, error_open
        mov si, error_open 
        ;
        ;
        ;mov dx, error_read
        mov si, error_read  
        ;
        ;
        ;mov al, '$'   
        mov al, 0                 
        ;
        ;
        ;mov dx, filedata
        mov si, filedata  
        ;
        ;
        error_open db "File not found.",0 
        error_read db "Can't read file.",0 
    
Post 12 Oct 2012, 21:19
View user's profile Send private message Visit poster's website Reply with quote
sbeng



Joined: 11 Oct 2012
Posts: 12
sbeng 13 Oct 2012, 08:32
EDIT
Hi, thank you for your replies.
I've modified my code. Now i've resolved NTVDM error but during execute my program it does not show Error_String for path error. Sad

Code:


format MZ
entry main:start            ; program entry point
stack 100h                ; stack size

segment main                ; main program segment
start:

;read file and save content in memory array
call OPEN_FILE
jc PRINT_STATUS_KO
mov [HANDLE16],AX
call PRINT_STATUS_OK

segment text
HANDLE16  DW 0
File_CH16 db 'C:\download\test\ciaao.txt$'
Error_String db 'Errore$'
OK db 'OK$'
segment extra

OPEN_FILE:
        push dx
        mov ah, 3Dh
        mov al, 00h
        mov dx, File_CH16
        int 21h
        pop dx
        ret
CLOSE_ROM:
       push bx
        mov ah, 3Eh
        mov bx, [HANDLE16]
        int 21h
        pop bx
        ret

PRINT_STATUS_OK:
       mov    dx,OK
       call    extra:write_text
       call extra:CLOSE_ROM
       jmp END_PROCESS
PRINT_STATUS_KO:
       mov    dx,Error_String
       call    extra:write_text
       call extra:CLOSE_ROM
       jmp END_PROCESS
END_PROCESS:
        mov ax,4Ch
        int 21h
 write_text:
        push ax
        mov ah, 09h
        int 21h
        pop ax
        ret
    
Very Happy Very Happy


Last edited by sbeng on 13 Oct 2012, 08:52; edited 1 time in total
Post 13 Oct 2012, 08:32
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20302
Location: In your JS exploiting you and your system
revolution 13 Oct 2012, 08:39
You will need to use ret, instead of retf.
Post 13 Oct 2012, 08:39
View user's profile Send private message Visit poster's website Reply with quote
sinsi



Joined: 10 Aug 2007
Posts: 789
Location: Adelaide
sinsi 14 Oct 2012, 02:08
An EXE is segmented, you need to set up at least DS
Code:
start:
  mov ax,text
  mov ds,ax
    
Post 14 Oct 2012, 02:08
View user's profile Send private message Reply with quote
sbeng



Joined: 11 Oct 2012
Posts: 12
sbeng 14 Oct 2012, 08:00
Now is my updated code. Why it doesn't print error message? Sad
It does not show CPU error but it does show only a cursor.
Thank you for your patience:

Code:
format MZ
entry main:start            ; program entry point
stack 100h                ; stack size

segment main                ; main program segment
start:
  mov ax,text
  mov ds,ax
;read file and save content in memory array
call OPEN_FILE
jc PRINT_STATUS_KO
mov [HANDLE16],AX
call PRINT_STATUS_OK

segment text
HANDLE16  DW 0
File_CH16 db "C:\download\test\ciaao.txt",0
Error_String db "Errore$"
OK db "OK$"
segment extra

OPEN_FILE:
        push dx
        mov ah, 3Dh
        mov al, 00h
        mov dx, File_CH16
        int 21h
        pop dx
        ret
CLOSE_ROM:
       push bx
        mov ah, 3Eh
        mov bx, [HANDLE16]
        int 21h
        pop bx
        ret

PRINT_STATUS_OK:
       mov  dx,OK
       call write_text
       call CLOSE_ROM
       jmp END_PROCESS
PRINT_STATUS_KO:
       mov   dx,Error_String
       call  write_text
       call  CLOSE_ROM
       jmp END_PROCESS
END_PROCESS:
        mov ax,4Ch
        int 21h
 write_text:
        push ax
        mov ah, 09h
        int 21h
        pop ax
        ret
    
Post 14 Oct 2012, 08:00
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20302
Location: In your JS exploiting you and your system
revolution 14 Oct 2012, 08:29
Something like this might be suitable:
Code:
format MZ
entry main:start            ; program entry point
stack 100h                ; stack size

segment main                ; main program segment
start:
       mov ax,text
 mov ds,ax
;read file and save content in memory array
    call OPEN_FILE
      jc PRINT_STATUS_KO
  mov [HANDLE16],AX
   call PRINT_STATUS_OK
        jmp END_PROCESS

OPEN_FILE:
        push dx
        mov ah, 3Dh
        mov al, 00h
        mov dx, File_CH16
        int 21h
        pop dx
        ret
CLOSE_ROM:
       push bx
        mov ah, 3Eh
        mov bx, [HANDLE16]
        int 21h
        pop bx
        ret

PRINT_STATUS_OK:
       mov  dx,OK
       call write_text
       call CLOSE_ROM
       jmp END_PROCESS
PRINT_STATUS_KO:
       mov   dx,Error_String
       call  write_text
       call  CLOSE_ROM
       jmp END_PROCESS
END_PROCESS:
        mov ax,4Ch
        int 21h
 write_text:
        push ax
        mov ah, 09h
        int 21h
        pop ax
        ret

segment text
HANDLE16  DW 0
File_CH16 db "C:\download\test\ciaao.txt",0
Error_String db "Errore$"
OK db "OK$"    
Post 14 Oct 2012, 08:29
View user's profile Send private message Visit poster's website Reply with quote
sbeng



Joined: 11 Oct 2012
Posts: 12
sbeng 15 Oct 2012, 13:16
Sorry for my bad coding Laughing
Now i've modified my code but it doesn't appear no Error_String text.

Code:
format MZ

entry main:start            ; program entry point
segment main                ; main program segment
include "c:\download\fasm\include\macro\if.inc"

start:
        mov ax,text
        mov ds,ax
;read file and save content in memory array
        call OPEN_FILE
       .if ( CARRY? )
            mov   dx,Error_String
            call write_text
            call CLOSE_ROM
            jmp END_PROCESS
        .endif

        mov [HANDLE16], ax
        call CLOSE_ROM
        END_PROCESS:
                mov ax,4Ch
                int 21h

segment extra
OPEN_FILE:
       push dx
        mov ah, 3Dh
        mov al, 00h
        mov dx, filename
        int 21h
        pop dx
        ret
CLOSE_ROM:
       push bx
        mov ah, 3Eh
        mov bx, [HANDLE16]
        int 21h
        pop bx
        ret

PRINT_STATUS_OK:
       mov  dx,OK
       call write_text
       call CLOSE_ROM
       call END_PROCESS
       ret
PRINT_STATUS_KO:
       mov   dx,Error_String
       call  write_text
       call  CLOSE_ROM
       call END_PROCESS
       ret
 write_text:
        push ax
        mov ah, 09h
        int 21h
        pop ax
        ret

segment text
HANDLE16  DW 0
filename db "C:\download\test\ciaao.txt",0
Error_String db "Errore$"
OK db "OK$"
    
Sad
Post 15 Oct 2012, 13:16
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20302
Location: In your JS exploiting you and your system
revolution 15 Oct 2012, 13:18
Try removing the "segment extra" line.
Post 15 Oct 2012, 13:18
View user's profile Send private message Visit poster's website Reply with quote
sbeng



Joined: 11 Oct 2012
Posts: 12
sbeng 15 Oct 2012, 13:28
revolution wrote:
Try removing the "segment extra" line.


Removing this line, executing my code it does appear a crash
Post 15 Oct 2012, 13:28
View user's profile Send private message Reply with quote
ctl3d32



Joined: 30 Dec 2009
Posts: 206
Location: Brazil
ctl3d32 15 Oct 2012, 14:12
null terminate your strings
Post 15 Oct 2012, 14:12
View user's profile Send private message Reply with quote
sbeng



Joined: 11 Oct 2012
Posts: 12
sbeng 16 Oct 2012, 12:00
Nothing Sad
My code does show only cursor and doesn't write nothing. What is difference between call and jump.
Thank you for all!


Code:
format MZ

entry main:start            ; program entry point
segment main                ; main program segment
include "c:\download\fasm\include\macro\if.inc"

start:
        mov ax,text
        mov ds,ax
;read file and save content in memory array
        call OPEN_FILE
       .if ( CARRY? )
            mov   dx,Error_String
            call write_text
            call CLOSE_ROM
            jmp END_PROCESS
        .endif

        mov [HANDLE16], ax
        call CLOSE_ROM
        END_PROCESS:
                mov ax,4Ch
                int 21h

segment extra
OPEN_FILE:
       push dx
        mov ah, 3Dh
        mov al, 00h
        mov dx, filename
        int 21h
        pop dx
        ret
CLOSE_ROM:
       push bx
        mov ah, 3Eh
        mov bx, [HANDLE16]
        int 21h
        pop bx
        ret

PRINT_STATUS_OK:
       mov  dx,OK
       call write_text
       call CLOSE_ROM
       call END_PROCESS
       ret
PRINT_STATUS_KO:
       mov   dx,Error_String
       call  write_text
       call  CLOSE_ROM
       call END_PROCESS
       ret
 write_text:
        push ax
        mov ah, 09h
        int 21h
        pop ax
        ret

segment text
HANDLE16  DW 0
filename db "C:\download\test\ciaao.txt",0
Error_String db "Errore",0
OK db "OK",0
    
Post 16 Oct 2012, 12:00
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20302
Location: In your JS exploiting you and your system
revolution 16 Oct 2012, 12:10
Remove the "segment extra" line for a start. Then try with the different string formats. Start simple with a "hello world!" and work up from there.
Post 16 Oct 2012, 12:10
View user's profile Send private message Visit poster's website Reply with quote
AsmGuru62



Joined: 28 Jan 2004
Posts: 1619
Location: Toronto, Canada
AsmGuru62 16 Oct 2012, 12:38
@sbeng:

1. How important is it for you to have a multi-segmented EXE program?
COM file would be so much simpler in this case. Or you're
preparing to code a very large program? I am asking, because with
a COM file you still can code a program, where data (but not code) may
take as much as 500Kb or so. Code however is limited to 64Kb minus
(global variables + PSP), however you would be surprised how much of a functionality can
be fit into that room. A lot!

If I recall, Philippe Kahn (founder of Borland) has coded a full Pascal
Compiler into a COM file!

2. What exactly are you trying to do? You open file, then what?
Do you need read the file anywhere? Show it on console? Count
any characters in that file? What exactly do you need?

3. Watch out for string termination. When you need to PRINT the text
with (AH=9h / INT 21h) - the text MUST be terminated with a dollar
character, like so:

OK db 'OK$'

However, any file names are to be terminated by a zero byte. So,
terminators are different in case of file names and text to be printed on console.
Post 16 Oct 2012, 12:38
View user's profile Send private message Send e-mail Reply with quote
sbeng



Joined: 11 Oct 2012
Posts: 12
sbeng 16 Oct 2012, 13:44
AsmGuru62 wrote:
@sbeng:

1. How important is it for you to have a multi-segmented EXE program?
COM file would be so much simpler in this case. Or you're
preparing to code a very large program? I am asking, because with
a COM file you still can code a program, where data (but not code) may
take as much as 500Kb or so. Code however is limited to 64Kb minus
(global variables + PSP), however you would be surprised how much of a functionality can
be fit into that room. A lot!

If I recall, Philippe Kahn (founder of Borland) has coded a full Pascal
Compiler into a COM file!

2. What exactly are you trying to do? You open file, then what?
Do you need read the file anywhere? Show it on console? Count
any characters in that file? What exactly do you need?

3. Watch out for string termination. When you need to PRINT the text
with (AH=9h / INT 21h) - the text MUST be terminated with a dollar
character, like so:

OK db 'OK$'

However, any file names are to be terminated by a zero byte. So,
terminators are different in case of file names and text to be printed on console.



OK,
now i will use COM format so i can to focus only functions of my code.
I would write some example in ASM (FASM) to improve my ASM knowledge.
In this case, i want load a file (txt or binary), if i doesn't find it i need to print a message otherwise i could open it.
Post 16 Oct 2012, 13:44
View user's profile Send private message Reply with quote
sbeng



Joined: 11 Oct 2012
Posts: 12
sbeng 16 Oct 2012, 14:17
Ok i have win Very Happy
Now my code does print "OK", but if you modify path it does print "Errore".
Thank you for all!

Code:
org 100h
start:
include "c:\download\fasm\include\macro\if.inc"

;read file and save content in memory array
        call OPEN_FILE
       .if ( CARRY? )
            mov   dx,Error_String
            call PRINT_STATUS_KO
        .endif

        mov [HANDLE16], ax
        call PRINT_STATUS_OK

        END_PROCESS:
                mov ax,4Ch
                int 21h
                 ret
OPEN_FILE:
        mov ah, 3Dh
        mov al, 00h
        mov dx, filename
        int 21h
        ret
CLOSE_ROM:
        mov ah, 3Eh
        mov bx, [HANDLE16]
        int 21h
        ret

PRINT_STATUS_OK:
       mov  dx,OK
       call write_text
       call CLOSE_ROM
       call END_PROCESS
       ret
PRINT_STATUS_KO:
       mov   dx,Error_String
       call  write_text
       call  CLOSE_ROM
       call END_PROCESS
       ret
 write_text:
        mov ah, 09h
        int 21h
        ret

HANDLE16  DW 0
filename db "C:\download\test\ciao.txt",0
Error_String db "Errore$"
OK db "OK$"
    
Post 16 Oct 2012, 14:17
View user's profile Send private message Reply with quote
AsmGuru62



Joined: 28 Jan 2004
Posts: 1619
Location: Toronto, Canada
AsmGuru62 16 Oct 2012, 15:30
Smile
Status KO reminds me of old Arcade games where you can play Street Fighter and get KO-ed.
Also, that scene from "BloodSport" where Frank Dux played with the big biker guy.
Sweet!
Post 16 Oct 2012, 15:30
View user's profile Send private message Send e-mail Reply with quote
sbeng



Joined: 11 Oct 2012
Posts: 12
sbeng 20 Oct 2012, 09:47
Hi,
now i would read file content Smile
I'm reading interrupt docs:

Quote:

AH = 3Fh - "READ" - READ FROM FILE OR DEVICE

Entry:

BX = file handle
CX = number of bytes to read
DS:DX -> buffer for data


So, i've written this code also copying rCX post Smile

Quote:

READ_ROM:
mov bx,ax
mov cx,n_byte_read
mov dx,FileData ;dx = place to put text
mov ah,3Fh
int 21h
;"$" terminate text.
mov di,ax ;di = ActualFileSize
add di,FileData ;di = FileData + Actual File Size
mov al,"$" ;al = "$" terminator
stosb
ret

n_byte_read = 1
FileData db n_byte_read+1 dup (?)


What means stosb instruction?
Best regards.
Post 20 Oct 2012, 09:47
View user's profile Send private message Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  
Goto page 1, 2  Next

< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2024, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.