Tag: technology

  • Writing to video RAM

    One of the reasons DOS is the best platform for learning Intel Assembly language in my opinion is that it doesn’t prevent you from writing directly to video memory. People are unaware how much operating systems like Windows, and, to a lesser extent, Linux place limits on what you are allowed to do. The idea is that most people are not smart enough to be trusted with arbitrarily writing to video RAM, devices, etc.

    But that is where DOSBox comes in. Since it runs DOS inside an emulator, there is no danger. The program I am going to show you today is long and complicated but it does something that I can’t even do on Linux, write directly to video memory in multiple colors.

    Here is the source code that made all of that possible! I tested it to assemble with either FASM or NASM. If you assemble it and run it in DOSBox, or even a real DOS system, you will get something that looks like the picture above.

    org 100h
    
    main:
    
    ;set up the extra segment at the beginning of the program
    ;to point to the video memory segment in DOS text mode
    mov ax, 0xB800
    mov es, ax ; Or mov ds, ax
    
    
    ;80 columes times 25 rows is 2000 chars
    ;but since each character is two bytes
    ;4000 is the number of bytes to erase
    ;whatever character we write in this loop will fill the whole screen!
    
    mov bx,0
    screen_clear:
    mov [es:bx],word 0x0403
    add bx,2
    cmp bx,4000
    jnz screen_clear
    
    mov ax,title  ;the string we intend to write to video RAM
    mov ch,0x0F   ;the character attribute
    mov dx,0x0218
    call putstring_vram
    
    mov ax,v_str  ;the string we intend to write to video RAM
    mov ch,0x70   ;the character attribute
    mov dx,0x0401
    call putstring_vram
    
    ;set the starting attribute for characters and location
    mov ch,0x01   ;the character attribute
    mov dx,0x0501 ;x,y position of where text should start on screen
    
    loop_vram:
    cmp ch,0x10
    jz loop_vram_end
    mov ax,v_str  ;the string we intend to write to video RAM
    call putstring_vram
    add dx,0x100
    inc ch
    jmp loop_vram
    loop_vram_end:
    
    mov ax,4C00h
    int 21h
    
    title db 'Chastity Video RAM Demonstration!',0
    v_str db 'Hello World! This string will be written to video RAM using Assembly language!',0
    
    ;Unlike previous functions I wrote that use DOS interrupts to write text to the screen
    ;this one makes use of several registers which are not meant to be preserved
    ;registers ax,cx,and dx must be set before calling this function
    
    ;ax = address of string to write
    ;bx = copied from ax and used to index the string
    ;cx = used for character attribute(ch) and value(cl)
    ;dx = column(x pos) and row(y pos) of where string should be printed
    
    ;For this routine, I chose to copy the dx register to memory locations for clarity
    ;Yes, it wastes some bytes but at least I can read it as I am familiar with x,y coordinates
    ;Most importantly, the dx register is never modified in this function
    ;This is important because the main program may need to modify it in a loop
    ;For writing data in consecutive rows (e.g. integer sequences)
    
    x db 0
    y db 0
    
    putstring_vram:
    
    mov bx,ax             ;copy ax to bx for use as index register
    
    ;get x and y positions from each byte of dx register
    mov [x],dl
    mov [y],dh
    
    mov ax,80  ;set ax to 80 because there are 80 chars per row in text mode
    mul byte [y]    ;multiply with the y value
    mov byte [y],0  ;zero the y byte so we can add a 16 bit x value to ax
    add ax, word [x]
    
    shl ax,1 ;shift left once to account for two bytes per character
    
    mov di,ax ;we will use di as our starting output location
    
    putstring_vram_strlen_start:    ;this loop finds the length of the string as part of the putstring function
    
    cmp [bx],byte 0                 ;compare this byte with 0
    jz putstring_vram_strlen_end    ;if comparison was zero, jump to loop end because we have found the length/end of string
    mov cl,[bx]                     ;mov this character to cl
    mov [es:di],cx                  ;mov character and attribute set in ch(before calling this function) to extra_segment+di
    add di,2                        ;each character contains two bytes (ASCII+Attribute). We must add two here.
    inc bx                          ;increment bx to point to next character
    jmp putstring_vram_strlen_start ;jump to the start of the loop and keep trying until we find a zero
    
    putstring_vram_strlen_end:
    
    ret
    
  • arithmetic by bitwise operators

    I have started a new programming library in C which simulates arithmetic using only bitwise operators. This is purely for computer science purposes and to prove that it can be done. The hardest part was getting the division function working correctly. I can prove that these functions work with another program I wrote, but I really want to record a video sometime to show the process of how these work.

    For now, here is the source of the 4 arithmetic functions and some global variables used in the division function.

    /*
     bitlib.h
    
     This library simulates the four arithmetic functions: ( addition, subtraction, multiplication, and division )
     Using only bitwise operations: ( AND, OR, XOR, SHL, SHR )
     
     Most of the time I would not need to do this, however, there exist applications where this information may prove useful.
     
     - Programming ARM CPUs which don't have a division instruction.
     - Arbitary Precision Arithmetic involving thousands of digits.
    
    */
    
    int add(int di,int si)
    {
     while(si!=0)
     {
      int ax=di;
      di^=si;
      si&=ax;
      si<<=1;
     }
     return di;
    }
    
    
    int sub(int di,int si)
    {
     while(si!=0)
     {
      di^=si;
      si&=di;
      si<<=1;
     }
     return di;
    }
    
    
    
    int mul(int di,int si)
    {
     int ax=0;
     while(si!=0)
     {
      if((si&1)!=0){ax=add(ax,di);}
      di<<=1;
      si>>=1;
     }
     return ax;
    }
    
    /*
    this division function returns the quotient, but also stores the remainder of division in a global variable
    */
    
    int sign_bit=1<<((sizeof(int)<<3)-1); /*used to extract the most significant bit during division function*/
    
    int mod=0; /*to store the modulus/remainder of the division function*/
    
    int bitdiv(int di,int si)
    {
     int ax=0,bx=0,cx=1;
     if(si==0){return 0;} /*division by zero is invalid*/
    
     while(cx!=0)
     {
      ax<<=1;
      bx<<=1;
      if(di&sign_bit){bx|=1;}
      di<<=1;
      
      if(bx>=si)
      {
       bx=sub(bx,si);
       ax|=1;
      }
     
      cx<<=1;
     }
    
     mod=bx;
     return ax;
    }
    
  • Chastity’s Source for ELF 64-bit executable creation

    I did it again. I decoded the 64 bit ELF header format for running an Assembly program on Intel CPUs running Linux. Although I don’t usually do 64 bit programming, I do have references which would allow me to translated my previous programs to use the 64-bit registers. Most of the time this is not needed for what they do.

    ;Chastity's Source for ELF 64-bit executable creation
    ;
    ;All data as defined in this file is based off of the specification of the ELF file format.
    ;I first looked at the type of file created by FASM's "format ELF executable" directive.
    ;It is great that FASM can create an executable file automatically. (Thanks Tomasz Grysztar, you are a true warrior!)
    ;However, I wanted to understand the format for theoretical use in other assemblers like NASM.
    
    ;The Github repository with the spec I used is here.
    ;<https://github.com/xinuos/gabi>
    ;And this is the wikipedia article which linked me to the specification document
    ;<https://en.wikipedia.org/wiki/Executable_and_Linkable_Format>
    
    ;This rest of this file contains a raw binary ELF64 header created using db,dw,dd,dq commands.
    ;After that, it proceeds to assemble a real "Hello World!" program
    
    ;Header for 64 bit ELF executable (with comments based on specification)
    
    db 0x7F,"ELF" ;ELFMAGIC: 4 bytes that identify this as an ELF file. The magic numbers you could say.
    db 2          ;EI_CLASS: 1=32-bit 2=64-bit
    db 1          ;EI_DATA: The endianness of the data. 1=ELFDATA2LSB 2=ELFDATA2MSB For Intel x86 this is always 1 as far as I know.
    db 1          ;EI_VERSION: 1=EV_CURRENT (ELF identity version 1) (which is current at time of specification Version 4.2 I was using)
    db 9 dup 0    ;padding zeros to bring us to address 0x10
    dw 2          ;e_type: 2=ET_EXEC (executable instead of object file)
    dw 0x3E       ;e_machine : 3=EM_386 (Intel 80386) 0x3E (AMD x86-64 architecture)
    dd 1          ;e_version: 1=EV_CURRENT (ELF object file version.)
    
    p_vaddr=0x400000
    e_entry=0x400078 ;we will be reusing this constant later 
    
    dq e_entry    ;e_entry: the virtual address at which the program starts
    dq 0x40       ;e_phoff: where in the file the program header offset is
    db 12 dup 0   ;e_shoff and e_flags are unused in this example,therefore all zeros
    dw 0x40       ;e_ehsize: size of the ELF header
    dw 0x38       ;e_phentsize: size of program header which happens after ELF header
    dw 1          ;e_phnum: How many program headers. Only 1 in this case
    dw 0x40       ;e_shentsize: Size of a section header
    dw 0          ;e_shnum number of section headers
    dw 0          ;e_shstrndx: section header string index (not used here)
    
    ;That is the end of the 0x40 byte (64 bytes decimal) ELF header. Sadly, this is not the end and a program header is also required (what drunk person made this format?)
    
    dd 1           ;p_type: 1=PT_LOAD
    dd 7           ;p_flags: permission flags: 7=4(Read)+2(Write)+1(Execute)
    dq 0           ;p_offset: Base address from file (zero)
    dq p_vaddr     ;p_vaddr: Virtual address in memory where the file will be.
    dq p_vaddr     ;p_paddr: Physical address. Same as previous
    
    image_size=0x1000 ;Chosen size for file and memory size. At minimum this must be as big as the actual binary file (code after header included)
                      ;By choosing a default size of 0x1000, I am assuming all assembly programs I write will be less than 4 kilobytes
    
    dq image_size  ;p_filesz: Size of file image of the segment. Must be equal to the file size or greater
    dq image_size  ;p_memsz: Size of memory image of the segment, which may be equal to or greater than file image.
    
    dq 0           ;p_align; Alignment (none)
    
    ;important FASM directives
    use64          ;tell assembler that 64 bit code is being used
    org e_entry    ;origin of new code begins at the entry point
    
    ;now, the actual hello world program
    mov rax,1   ; invoke SYS_WRITE (kernel opcode 1 on 64 bit systems)
    mov rdi,1   ; write to the STDOUT file
    mov rsi,msg ; pointer/address of string to write
    mov rdx,13  ; number of bytes to write
    syscall
    
    mov rax,0x3C ; invoke SYS_EXIT (kernel opcode 0x3C on 64 bit systems)
    mov rdi,0    ; return 0 status on exit - 'No Errors'
    syscall
    
    msg db 'Hello World!',0Ah
    
    ;This is the makefile I use when assembling and running this program
    
    ;main-fasm:
    ;	fasm ELF-64-hello.asm
    ;	chmod +x ELF-64-hello.bin
    ;	./ELF-64-hello.bin
    
  • Chastity’s Source for ELF 32-bit executable creation

    I wrote an example program using a custom made ELF-32 header using data declaration statements. I wrote many comments which reference the official specification. This was an exercise both in programming and also Technical Reading and Writing. I had to read enough of the specification PDF file to understand what I was doing. I then tried to write descriptive comments that at the very least I would understand when I need to remind myself how this format is created.

    FASM Assembler 32-bit ELF Hello World

    ;Chastity's Source for ELF 32-bit executable creation
    ;
    ;All data as defined in this file is based off of the specification of the ELF file format.
    ;I first looked at the type of file created by FASM's "format ELF executable" directive.
    ;It is great that FASM can create an executable file automatically. (Thanks Tomasz Grysztar, you are a true warrior!)
    
    ;However, I wanted to understand the format for theoretical use in other assemblers like NASM.
    ;However, the syntax of FASM and NASM is still different enough that this will work only in FASM.
    ;I do have a NASM version in a separate file in my repository though.
    
    ;The Github repository with the spec I used is here.
    ;<https://github.com/xinuos/gabi>
    ;And this is the wikipedia article which linked me to the specification document
    ;<https://en.wikipedia.org/wiki/Executable_and_Linkable_Format>
    
    ;This file contains a raw binary ELF32 header created using db,dw,dd commands.
    ;After that, it proceeds to assemble a real "Hello World!" program
    
    ;Header for 32 bit ELF executable (with comments based on specification)
    
    db 0x7F,"ELF" ;ELFMAGIC: 4 bytes that identify this as an ELF file. The magic numbers you could say.
    db 1          ;EI_CLASS: 1=32-bit 2=64-bit
    db 1          ;EI_DATA: The endianness of the data. 1=ELFDATA2LSB 2=ELFDATA2MSB For Intel x86 this is always 1 as far as I know.
    db 1          ;EI_VERSION: 1=EV_CURRENT (ELF identity version 1) (which is current at time of specification Version 4.2 I was using)
    db 9 dup 0    ;padding zeros to bring us to address 0x10
    dw 2          ;e_type: 2=ET_EXEC (executable instead of object file)
    dw 3          ;e_machine : 3=EM_386 (Intel 80386) 0x3E (AMD x86-64 architecture)
    dd 1          ;e_version: 1=EV_CURRENT (ELF object file version.)
    
    p_vaddr  =  0x8048000 ;the absolute base address where the file is loaded into memory
    e_entry  =  0x8048054 ;program starts running at this address (right after header)
    
    dd e_entry    ;e_entry: the address at which the program starts running
    dd 0x34       ;e_phoff: where in the file the program header offset is
    db 8 dup 0    ;e_shoff and e_flags are unused in this example,therefore all zeros
    dw 0x34       ;e_ehsize: size of the ELF header
    dw 0x20       ;e_phentsize: size of program header which happens after ELF header
    dw 1          ;e_phnum: How many program headers. Only 1 in this case
    dw 0x28       ;e_shentsize: Size of a section header
    dw 0          ;e_shnum number of section headers
    dw 0          ;e_shstrndx: section header string index (not used here)
    
    ;That is the end of the 0x34 byte (52 bytes decimal) ELF header. Sadly, this is not the end and a program header is also required (what drunk person made this format?)
    
    dd 1          ;p_type: 1=PT_LOAD
    dd 0          ;p_offset: Base address from file (zero)
    dd p_vaddr    ;p_vaddr: Virtual address in memory where the file will be.
    dd p_vaddr    ;p_paddr: Physical address. Same as previous
    
    ;The file_size variable I have defined uses some trickery to get the size of the file.
    ;An EOF constant (End Of File) is defined at the end of the program code
    ;By subtracting the program virtual address from that address,
    ;I get the actual number of bytes of this entire program
    
    file_size equ EOF-p_vaddr ;Place the actual size of the file using NASM address constants
    
    dd file_size  ;p_filesz: Size of file image of the segment. Must be equal to the file size or greater
    dd file_size  ;p_memsz: Size of memory image of the segment, which may be equal to or greater than file image.
    
    dd 7           ;p_flags: permission flags: 7=4(Read)+2(Write)+1(Execute)
    dd 0x1000      ;p_align; Alignment (same page alignment that FASM uses of 4096 bytes)
    
    ;important FASM directives
    
    use32          ;tell assembler that 32 bit code is being used
    org e_entry    ;origin of new code begins here
    
    ;Now, the actual hello world program
    
    mov eax,4   ;invoke SYS_WRITE (kernel opcode 4 on 32 bit systems)
    mov ebx,1   ;write to the STDOUT file
    mov ecx,msg ;pointer/address of string to write
    mov edx,13  ;number of bytes to write
    int 80h
    
    mov eax,1   ;function SYS_EXIT (kernel opcode 1 on 32 bit systems)
    mov ebx,0   ;return 0 status on exit - 'No Errors'
    int 80h
    
    msg db 'Hello World!',0Ah,0
    
    EOF  =  $ ; define a label for the end of file. This is used in the ELF header
    
    ;To Assemble and run this program on Linux, you can use the following makefile.
    ;You can also disassemble it with ndisasm because the exact address of code is known
    
    ;main-fasm:
    ;	fasm ELF-32-hello.asm
    ;	chmod +x ELF-32-hello.bin
    ;	./ELF-32-hello.bin
    ;ndisasm:
    ;	ndisasm -b 32 -o 0x8048054 -e 0x54 ELF-32-hello.bin
    

    NASM Assembler 32-bit ELF Hello World

    ;Chastity's Source for ELF 32-bit executable creation
    ;
    ;All data as defined in this file is based off of the specification of the ELF file format.
    ;I first looked at the type of file created by FASM's "format ELF executable" directive.
    ;It is great that FASM can create an executable file automatically. (Thanks Tomasz Grysztar, you are a true warrior!)
    
    ;However, I wanted to understand the format for theoretical use in other assemblers like NASM.
    ;Therefore, what you see here is a complete Hello World program that should work within NASM
    ;to create an executable file without using a linker. It worked perfectly on my machine running Debian Linux and NASM version 2.16.01.
    
    ;The Github repository with the spec I used is here.
    ;<https://github.com/xinuos/gabi>
    ;And this is the wikipedia article which linked me to the specification document
    ;<https://en.wikipedia.org/wiki/Executable_and_Linkable_Format>
    
    ;This file contains a raw binary ELF32 header created using db,dw,dd commands.
    ;After that, it proceeds to assemble a real "Hello World!" program
    
    ;Header for 32 bit ELF executable (with comments based on specification)
    
    db 0x7F,"ELF" ;ELFMAGIC: 4 bytes that identify this as an ELF file. The magic numbers you could say.
    db 1          ;EI_CLASS: 1=32-bit 2=64-bit
    db 1          ;EI_DATA: The endianness of the data. 1=ELFDATA2LSB 2=ELFDATA2MSB For Intel x86 this is always 1 as far as I know.
    db 1          ;EI_VERSION: 1=EV_CURRENT (ELF identity version 1) (which is current at time of specification Version 4.2 I was using)
    db 9 dup 0    ;padding zeros to bring us to address 0x10
    dw 2          ;e_type: 2=ET_EXEC (executable instead of object file)
    dw 3          ;e_machine : 3=EM_386 (Intel 80386) 0x3E (AMD x86-64 architecture)
    dd 1          ;e_version: 1=EV_CURRENT (ELF object file version.)
    
    p_vaddr equ 0x8048000 ;the absolute base address where the file is loaded into memory
    e_entry equ 0x8048054 ;program starts running at this address (right after header)
    
    dd e_entry    ;e_entry: the address at which the program starts running
    dd 0x34       ;e_phoff: where in the file the program header offset is
    db 8 dup 0    ;e_shoff and e_flags are unused in this example,therefore all zeros
    dw 0x34       ;e_ehsize: size of the ELF header
    dw 0x20       ;e_phentsize: size of program header which happens after ELF header
    dw 1          ;e_phnum: How many program headers. Only 1 in this case
    dw 0x28       ;e_shentsize: Size of a section header
    dw 0          ;e_shnum number of section headers
    dw 0          ;e_shstrndx: section header string index (not used here)
    
    ;That is the end of the 0x34 byte (52 bytes decimal) ELF header. Sadly, this is not the end and a program header is also required (what drunk person made this format?)
    
    dd 1          ;p_type: 1=PT_LOAD
    dd 0          ;p_offset: Base address from file (zero)
    dd p_vaddr    ;p_vaddr: Virtual address in memory where the file will be.
    dd p_vaddr    ;p_paddr: Physical address. Same as previous
    
    ;The file_size variable I have defined uses some trickery to get the size of the file.
    ;An EOF constant (End Of File) is defined at the end of the program code
    ;By subtracting the program virtual address from that address,
    ;I get the actual number of bytes of this entire program
    
    file_size equ EOF-p_vaddr ;Place the actual size of the file using NASM address constants
    
    dd file_size  ;p_filesz: Size of file image of the segment. Must be equal to the file size or greater
    dd file_size  ;p_memsz: Size of memory image of the segment, which may be equal to or greater than file image.
    
    dd 7           ;p_flags: permission flags: 7=4(Read)+2(Write)+1(Execute)
    dd 0x1000      ;p_align; Alignment (same page alignment that FASM uses of 4096 bytes)
    
    ;important NASM directives
    
    use32          ;tell assembler that 32 bit code is being used
    org p_vaddr    ;origin of new code begins here
    
    ;Now, the actual hello world program
    
    mov eax,4   ;invoke SYS_WRITE (kernel opcode 4 on 32 bit systems)
    mov ebx,1   ;write to the STDOUT file
    mov ecx,msg ;pointer/address of string to write
    mov edx,13  ;number of bytes to write
    int 80h
    
    mov eax,1   ;function SYS_EXIT (kernel opcode 1 on 32 bit systems)
    mov ebx,0   ;return 0 status on exit - 'No Errors'
    int 80h
    
    msg db 'Hello World!',0Ah,0
    
    EOF equ $ ; define a label for the end of file. This is used in the ELF header
    
    ;To Assemble and run this program on Linux, you can use the following makefile.
    ;You can also disassemble it with ndisasm because the exact address of code is known
    
    ;main-nasm:
    ;	nasm ELF-32-hello.asm
    ;	chmod +x ELF-32-hello
    ;	./ELF-32-hello
    ;ndisasm:
    ;	ndisasm -b 32 -o 0x8048054 -e 0x54 ELF-32-hello
    

    I made a repository for examples like this. Others may want to understand the header used on Linux systems.

    https://github.com/chastitywhiterose/ELF

  • New DOS program: chastehex

    I finally rewrote my chastehex program in DOS Assembly Language. Unlike the Linux version, it is not prepared to handle files over 64 kilobytes. There may be ways to improve upon it but writing this program was more of a proof of concept than anything because I already wrote the C version and the Linux 32 bit Assembly version. When this is assembled using FASM, it becomes a .com file which can be run inside DOSBox.

    If no arguments are used when running the program, it will display a brief message explaining the 3 modes of usage.

    org 100h     ;DOS programs start at this address
    
    mov word [radix],16 ; can choose radix for integer output!
    
    mov ch,0     ;zero ch (upper half of cx)
    mov cl,[80h] ;load length of the command string
    cmp cx,0
    jnz args_exist
    
    mov ax,help
    call putstring
    
    jmp ending
    
    args_exist:
    mov dx,81h   ;Point dx to the beginning of string
    inc dx       ;go to next char
    dec cx       ;but subtract 1 from count
    mov [arg_index],dx ;save index to variable so dx is free to change as needed
    
    ;find the end of the string based on length
    mov ax,dx
    add ax,cx
    ;now we know where the string ends.
    mov [arg_string_end],ax ;this is the end of the arg string. important for later
    ;call putint ; print address where entire arg string ends
    
    ;this routine replaces all non printable characters with zero in the arg string
    mov bx,dx
    filter:
    cmp byte [bx],' '
    ja notspace ; if char is above space, leave it alone
    mov byte [bx],0 ;otherwise it counts as a space, change it to a zero
    notspace:
    inc bx
    cmp bx,[arg_string_end] ;are we at the end of the arg string?
    jnz filter ;if not at end, continue the filter
    
    filter_end:
    mov byte [bx],0 ;terminate the ending with a zero for safety
    
    ;now that the argument string is prepared, we will try to use the first argument as a filename to open
    
    mov ah,3Dh ;call number for DOS open existing file
    mov al,2   ;file access: 0=read,1=write,2=read+write
    mov dx,[arg_index] ;string address to interpret as filename
    int 21h ;DOS call to finalize open function
    
    mov [file_handle],ax
    
    jc file_error ;if carry flag is set, we have an error, otherwise, file is open
    
    file_opened:
    mov ax,dx
    call putstring
    call putline
    ;mov ax,file_opened_message
    ;call putstring
    ;mov ax,[file_handle]
    ;call putint
    jmp use_file
    
    ;this section prints error message and then ends the program if file error found
    file_error: ;prints error code2=file not found
    mov ax,dx
    call putstring
    call putline
    mov ax,file_error_message
    call putstring
    mov ax,[file_handle]
    call putint
    jmp arg_loop_end
    
    ;how we use the file depends on the number of arguments given
    ;if no arguments other than the filename exist, we do a regular hex dump
    use_file:
    
    call get_next_arg ;get address of next arg and return into ax register
    cmp ax,[arg_string_end] ;this time, if ax equals end of string, we hex dump and then end the program later
    jz hexdump ;jump to hexdump section
    
    ;otherwise, if there are more args, as contains next arg
    ;then we use the strint function to transform it into a number
    ;call putstring
    ;call putline
    
    call strint ;turn string at address ax into a number returned in ax
    ;call putint
    
    ;this number will be out new offset to seek to
    mov [file_offset],ax
    
    mov ah,42h           ;lseek call number
    mov al,0            ;seek origin 00h start of file,01h current file position,02h end of file
    mov bx,[file_handle]
    mov cx,0            ;upper word of offet
    mov dx,[file_offset]
    int 21h
    
    jc arg_loop_end ;end program if seek error (though I can't imagine how it would fail)
    
    ;check if there are any more args
    call get_next_arg
    cmp ax,[arg_string_end]
    jz dump_byte ;jump to dump_byte section and continue with read mode
    mov [int_newline],0 ;disable auto newline printing
    jmp arg_loop ;otherwise we jump to the arg loop and write the values as bytes starting at offset
    
    ;this next section is the reading mode that reads one byte. It only executes if we have not provided bytes to write to the new address
    ;because we have an argument for an address we will read only this byte and display it
    dump_byte:
    
    mov ah,3Fh           ;call number for read function
    mov bx,[file_handle] ;store file handle to read from in bx
    mov cx,1             ;we are reading only 1 byte
    mov dx,byte_array    ;store the bytes here
    int 21h
    
    mov cx,ax ;number of bytes read
    
    mov [int_newline],0 ;disable auto newline printing
    ;set width to 8 and display offset
    mov [int_width],8
    mov ax,[file_offset]
    call putint
    call putspace
    
    cmp cx,1
    jz not_eof ;skip past here as long as one byte was read otherwise show EOF
    mov ax,end_of_file
    call putstring
    jmp arg_loop_end
    not_eof:
    
    mov ah,0 ;zero upper half of ax
    mov al,[byte_array]
    
    mov [int_width],2
    call putint
    ;call putline
    
    jmp arg_loop_end ;we are done so we end the program
    
    hexdump:
    
    ;we start the loop with a call to read exactly 16 bytes
    
    mov ah,3Fh           ;call number for read function
    mov bx,[file_handle] ;store file handle to read from in bx
    mov cx,16            ;we are reading sixteen bytes
    mov dx,byte_array    ;store the bytes here
    int 21h
    
    ;call putint ;check the number of bytes read
    
    ;important note: the number of bytes read should be 16 or less and this is not an error
    ;zero is expected if we are at the end of the file.
    ;however, if it is zero, we print an EOF message and exit
    
    cmp ax,0
    jnz print_row
    mov ax,end_of_file
    call putstring
    jmp arg_loop_end
    
    print_row:
    
    mov cx,ax ;number of bytes read
    
    mov [int_newline],0 ;disable auto newline printing
    ;set width to 8 and display offset
    mov [int_width],8
    mov ax,[file_offset]
    call putint
    call putspace
    add [file_offset],cx ;next offset will show correctly
    
    mov ah,0 ;zero upper half of ax
    mov bx,byte_array
    
    mov [int_width],2
    
    print_byte:
    mov al,[bx]
    call putint
    call putspace
    inc bx
    dec cx
    cmp cx,0
    jnz print_byte
    call putline
    
    jmp hexdump ;jump back to hexdump and attempt another read of a row
    
    ;this loop processes the rest of the arguments
    ;it interprets each one as a byte to write to the current offset
    ;this loop should only execute if a file name and address have already been given
    arg_loop:
    mov ax,[arg_index] ;get address of current arg
    ;call putstring
    
    call strint ;turn string at address ax into a number returned in ax
    
    mov [byte_array],al
    
    mov ah,40h           ; select DOS function 40h write 
    mov bx,[file_handle] ;store file handle to write to in bx
    mov cx,1             ;write 1 byte to this file
    mov dx,byte_array    ;write from this address
    int 21h
    
    ;set width to 8 and display offset
    mov [int_width],8
    mov ax,[file_offset]
    inc [file_offset]
    call putint
    call putspace
    mov [int_width],2
    mov ah,0
    mov al,[byte_array]
    call putint
    call putline
    
    call get_next_arg ;get address of next arg and return into ax register
    
    cmp ax,[arg_string_end] ;if the ax register contains address of the end of args string, end program to avoid failure
    jz arg_loop_end
    jmp arg_loop
    
    arg_loop_end: ;this is the correct end of the program
    
    ;close the file if it is open
    mov ah,3Eh
    mov bx,[file_handle]
    int 21h
    
    ending:
    mov ax,4C00h ; Exit program
    int 21h
    
    arg_string_end dw 0
    arg_index dw 0
    file_error_message db 'Could not open the file! Error number: ',0
    file_opened_message db 'The file is open with handle: ',0
    file_handle dw 0
    read_error_message db 'Failure during reading of file. Error number: ',0
    end_of_file db 'EOF',0
    
    ;where we will store data from the file
    byte_array db 16 dup '?'
    file_offset dw 0
    
    
    ;function to move ahead to the next art
    ;only works after the filter has been applied to turn all spaces into zeroes
    
    get_next_arg:
    mov bx,[arg_index] ;dx has address of current arg
    find_zero:
    cmp byte [bx],0
    jz found_zero
    inc bx
    jmp find_zero ; this char is not zero, go to the next char
    found_zero:
    
    find_non_zero:
    cmp bx,[arg_string_end]
    jz arg_finish ;if bx is already at end, nothing left to find
    cmp byte [bx],0
    jnz arg_finish ;if this char is not zero we have found the next string!
    inc bx
    jmp find_non_zero ;otherwise, keep looking
    
    arg_finish:
    mov [arg_index],bx ; save this index to variable
    mov ax,bx ;but also save it to ax register for use
    ret
    
    include 'chastelib16.asm'
    
    help db 'Welcome to chastehex! The tool for reading and writing bytes of a file!',0Ah
    db 'To hexdump an entire file:',0Ah,9,'chastehex file',0Ah
    db 'To read a single byte at an address:',0Ah,9,'chastehex file address',0Ah
    db 'To write a single byte at an address:',0Ah,9,'chastehex file address value',0Ah
    db 'The file must exist before you launch the program.',0Ah
    db 'This design was to prevent accidentally opening a mistyped filename.',0Ah,0
    

    Below is what is inside the ‘chastelib16.asm’ file which is included by the above source file. I keep it separate because it is my own standard library which was designed for everything needed to build chastehex. Routines for printing strings and numbers are the largest part. It also has the strint function which converts strings into numbers. That is how it interprets the second and beyond arguments as hexadecimal numbers.

    ; This file is where I keep my function definitions.
    ; These are usually my string and integer output routines.
    
    ;this is my best putstring function for DOS because it uses call 40h of interrupt 21h
    ;this means that it works in a similar way to my Linux Assembly code
    ;the plan is to make both my DOS and Linux functions identical except for the size of registers involved
    
    stdout dw 1 ; variable for standard output so that it can theoretically be redirected
    
    putstring:
    
    push ax
    push bx
    push cx
    push dx
    
    mov bx,ax                  ;copy ax to bx for use as index register
    
    putstring_strlen_start:    ;this loop finds the length of the string as part of the putstring function
    
    cmp [bx], byte 0           ;compare this byte with 0
    jz putstring_strlen_end    ;if comparison was zero, jump to loop end because we have found the length
    inc bx                     ;increment bx (add 1)
    jmp putstring_strlen_start ;jump to the start of the loop and keep trying until we find a zero
    
    putstring_strlen_end:
    
    sub bx,ax                  ; sub ax from bx to get the difference for number of bytes
    mov cx,bx                  ; mov bx to cx
    mov dx,ax                  ; dx will have address of string to write
    
    mov ah,40h                 ; select DOS function 40h write 
    mov bx,[stdout]            ; file handle 1=stdout
    int 21h                    ; call the DOS kernel
    
    pop dx
    pop cx
    pop bx
    pop ax
    
    ret
    
    
    
    ;this is the location in memory where digits are written to by the intstr function
    int_string db 16 dup '?' ;enough bytes to hold maximum size 16-bit binary integer
    ;this is the end of the integer string optional line feed and terminating zero
    ;clever use of this label can change the ending to be a different character when needed 
    int_newline db 0Dh,0Ah,0 ;the proper way to end a line in DOS/Windows
    
    radix dw 2 ;radix or base for integer output. 2=binary, 8=octal, 10=decimal, 16=hexadecimal
    int_width dw 8
    
    intstr:
    
    mov bx,int_newline-1 ;find address of lowest digit(just before the newline 0Ah)
    mov cx,1
    
    digits_start:
    
    mov dx,0;
    div word [radix]
    cmp dx,10
    jb decimal_digit
    jge hexadecimal_digit
    
    decimal_digit: ;we go here if it is only a digit 0 to 9
    add dx,'0'
    jmp save_digit
    
    hexadecimal_digit:
    sub dx,10
    add dx,'A'
    
    save_digit:
    
    mov [bx],dl
    cmp ax,0
    jz intstr_end
    dec bx
    inc cx
    jmp digits_start
    
    intstr_end:
    
    prefix_zeros:
    cmp cx,[int_width]
    jnb end_zeros
    dec bx
    mov [bx],byte '0'
    inc cx
    jmp prefix_zeros
    end_zeros:
    
    mov ax,bx ; store string in ax for display later
    
    ret
    
    
    
    ;function to print string form of whatever integer is in eax
    ;The radix determines which number base the string form takes.
    ;Anything from 2 to 36 is a valid radix
    ;in practice though, only bases 2,8,10,and 16 will make sense to other programmers
    ;this function does not process anything by itself but calls the combination of my other
    ;functions in the order I intended them to be used.
    
    putint: 
    
    push ax
    push bx
    push cx
    push dx
    
    call intstr
    call putstring
    
    pop dx
    pop cx
    pop bx
    pop ax
    
    ret
    
    
    
    
    
    
    
    
    ;this function converts a string pointed to by eax into an integer returned in eax instead
    ;it is a little complicated because it has to account for whether the character in
    ;a string is a decimal digit 0 to 9, or an alphabet character for bases higher than ten
    ;it also checks for both uppercase and lowercase letters for bases 11 to 36
    ;finally, it checks if that letter makes sense for the base.
    ;For example, G to Z cannot be used in hexadecimal, only A to F can
    ;The purpose of writing this function was to be able to accept user input as integers
    
    strint:
    
    mov bx,ax ;copy string address from ax to bx because eax will be replaced soon!
    mov ax,0
    
    read_strint:
    mov cx,0 ; zero ecx so only lower 8 bits are used
    mov cl,[bx]
    inc bx
    cmp cl,0 ; compare byte at address edx with 0
    jz strint_end ; if comparison was zero, this is the end of string
    
    ;if char is below '0' or above '9', it is outside the range of these and is not a digit
    cmp cl,'0'
    jb not_digit
    cmp cl,'9'
    ja not_digit
    
    ;but if it is a digit, then correct and process the character
    is_digit:
    sub cl,'0'
    jmp process_char
    
    not_digit:
    ;it isn't a digit, but it could be perhaps and alphabet character
    ;which is a digit in a higher base
    
    ;if char is below 'A' or above 'Z', it is outside the range of these and is not capital letter
    cmp cl,'A'
    jb not_upper
    cmp cl,'Z'
    ja not_upper
    
    is_upper:
    sub cl,'A'
    add cl,10
    jmp process_char
    
    not_upper:
    
    ;if char is below 'a' or above 'z', it is outside the range of these and is not lowercase letter
    cmp cl,'a'
    jb not_lower
    cmp cl,'z'
    ja not_lower
    
    is_lower:
    sub cl,'a'
    add cl,10
    jmp process_char
    
    not_lower:
    
    ;if we have reached this point, result invalid and end function
    jmp strint_end
    
    process_char:
    
    cmp cx,[radix] ;compare char with radix
    jae strint_end ;if this value is above or equal to radix, it is too high despite being a valid digit/alpha
    
    mov dx,0 ;zero edx because it is used in mul sometimes
    mul word [radix]    ;mul eax with radix
    add ax,cx
    
    jmp read_strint ;jump back and continue the loop if nothing has exited it
    
    strint_end:
    
    ret
    
    ;returns in al register a character from the keyboard
    getchr:
    
    mov ah,1
    int 21h
    
    ret
    
    ;the next utility functions simply print a space or a newline
    ;these help me save code when printing lots of things for debugging
    
    space db ' ',0
    line db 0Dh,0Ah,0
    
    putspace:
    push ax
    mov ax,space
    call putstring
    pop ax
    ret
    
    putline:
    push ax
    mov ax,line
    call putstring
    pop ax
    ret