assemblyx86-16masmmemory-segmentation

A2118: cannot have segment address references with TINY model


The task is to write an assembly language program that finds the first file in the directory and shows the time of its creation. When you launch the program, nothing happens. Where can the error be? Compilation is performed using MASM611 on DOSBox, using the command "ml.exe filename.asm".

.model tiny
.stack 100h

.data
    DTA db 128 dup(0)
    path db "C:\*.*", 0
    filename db 13 dup(0)
    creation_time db "File created on: ", 0

.code
main proc
    mov ax, @data
    mov ds, ax

    mov ah, 1Ah ; Set Disk Transfer Area (DTA)
    mov dx, offset DTA
    int 21h

    ; Set the path to the catalog
    mov dx, offset path
    mov ah, 4Eh ; Find First File
    int 21h

    ; Start the file search cycle
search_loop:
    ; Check if the file is found
    cmp al, 0 ; AL = 0, if the file is not found or everything has already been checked
    je exit_search

    ; Print the time of file creation
    mov dx, offset creation_time
    mov ah, 09h ; String output
    int 21h

    ; Upload the year and month of creation
    mov cx, word ptr [DTA+20h] ; Year
    mov bx, word ptr [DTA+22h] ; Month

    ; Print year
    mov ah, 02h ; Character output
    mov dl, ch ; Outputting the most significant byte (tens)
    add dl, 30h ; Convert a number to a character
    int 21h

    mov dl, cl ; Output of the low byte (one)
    add dl, 30h ; Convert a number to a character
    int 21h

    ; Print the month
    mov ah, 02h ; Character output
    mov dl, bh ; Outputting the most significant byte (tens)
    add dl, 30h ; Convert a number to a character
    int 21h

    mov dl, bl ; Output of the low byte (one) 
    add dl, 30h ; Convert a number to a character
    int 21h

    ; Print a new line
    mov dl, 0Dh ; CR
    int 21h
    mov dl, 0Ah ; LF
    int 21h

    ; Find the following file
    mov ah, 4Fh ; Find Next File
    int 21h

    jmp search_loop ; Repeat the file search cycle

exit_search:
    mov ah, 4Ch ; End the program
    int 21h

main endp
end main

If you compile using the command "ml.exe /AT filename.asm", next error occurs:

A2118: cannot have segment address references with TINY model

Maybe someone knows what the reason is?


Solution

  • It's complaining about mov ax, @data. Don't do that (or the mov ds, ax) in a .com executable, because there's no metadata for DOS to fill in the segment value.

    DS is already set correctly (to the same value as the other segments, that's what the tiny memory model is) when a .com executable starts. Use org 100h to tell the assembler what offset the top of your program will have relative to those segment bases, so offsets work correctly as absolute addresses.


    A .com executable is just a flat binary and execution begins at the top (first byte of the file). Sections aren't visible in the output file, but assemblers will put .code first, before .data, regardless of source order, so execution will begin at the top of your .code. So that's actually not a problem, thanks @Michael Petch for pointing that out.