Skip to content

Instantly share code, notes, and snippets.

@LightningStalker
Last active April 6, 2026 14:38
Show Gist options
  • Select an option

  • Save LightningStalker/ac73066c34cb4440bd15756b2ca948a0 to your computer and use it in GitHub Desktop.

Select an option

Save LightningStalker/ac73066c34cb4440bd15756b2ca948a0 to your computer and use it in GitHub Desktop.
DOS - Save image of disk A: in current directory to file DSK.IMG
name dskimg
page 55,132
title DSKIMG.COM--save a disk image
; Project Crew™ 1/18/2025
.MODEL tiny ; Create DSKIMG.COM
bytes equ 64 * 512 ; number of bytes in buffer
exit equ 4c00h ; exit back to DOS
dosfunc equ 21h ; DOS function call INT 21h
; Call Marcos
readKbd MACRO ; read keyboard char
mov ah, 8 ; function 8
int 21h
ENDM
putchar MACRO char ; print character to screen
mov dl, char ; the character
mov ah, 2 ; function 2
int 21h
ENDM
display MACRO string ; dicplays
mov dx, OFFSET string ; address of string
mov ah, 9 ; function 9
int 21h
ENDM
; Absolute disk read
absDiskRead MACRO disk, buffer, numSectors, start
mov al, disk ; drive to read from
mov bx, OFFSET buffer ; buffer address
mov cx, numSectors ; sectors to read
mov dx, start ; first sector
int 25h
jc error ; throw error
ENDM
; Write to disk file
writeFile MACRO fhandle, buffer, numBytes
mov ah, 40h ; function 40
; mov dx, seg buffer ; buffer address
; mov ds, dx
mov dx, offset buffer
mov bx, fhandle ; file handle
mov cx, numBytes ; length to write
int 21h ; transfer to MS-DOS
jc error ; jump, write failed
cmp ax, numBytes ; entire record written?
jne error ; no, jump
ENDM
.DATA
msg db "Source in A (1.4MiB)", 13, 10, '$' ; message texts
; db "Any Key to start. $"
errmsg db 13, 10, "an error occurred", '$'
fname db "DSK.IMG", 0 ; ASCIIZ filespec
crlf db 13, 10, '$'
start dw 0
fhandle dw ?
buffer db ? ;64 dup (512 dup (?)) ;64 sectors
_TEXT segment word public 'CODE'
org 100h ; .COM files always have
; an origin of 100h
; assume cs:_TEXT,ds:_TEXT,es:_TEXT,ss:_TEXT ; implicit in tiny
dskimg proc near
;malloc: mov ah,4ah ; function number
; mov bx,0600h ; new size (paragraphs)
; int 21h ; transfer to MS-DOS
; jc error ; jump, resize failed
; no need since DOS allocates all available memory to .COM programs
fcreate: mov ah, 3ch ; function number
xor cx, cx ; normal attribute
; mov dx, seg fname ; address of pathname
; mov ds, dx
mov dx, offset fname
int dosfunc ; transfer to MS-DOS
jc error ; jump if create failed
mov fhandle, ax ; save file handle
mkImg: display msg ; show message
mov cx, 46 ; set up blue bar
space: putchar ' ' ; go to end
loop space ; X = 46
putchar 0ddh ; end mark bracket
putchar 13 ; X = 0
putchar 0deh ; begin line
; readKbd ; wait for keypress
mov cx, 45 ; copy 45 groups of
; 64 sectors (1.4MiB)
copy: push cx ; save loop counter
absDiskRead 0, buffer, 64, start ; read drive A
writeFile fhandle, buffer, bytes ; write to file
add start, 64 ; advance to next sectors
pop cx ; get flags off stack
pop cx ; restore loop counter
putchar 0b1h ; progress bar
loop copy
theEnd: display crlf
mov ax, exit ; exit program
int dosfunc
error: display errmsg ; error known
jmp theEnd
dskimg endp
_TEXT ends
end dskimg ; defines entry point
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment