This is a video showing some very long source code of a DOS program I wrote using Assembly Language.
This program is technically a 16 bit DOS .com program and yet it can access 32 bit addresses of the file it operates on because the LSEEK DOS call uses CX:DX as a 32 bit address by using two 16 bit registers. By modifying several parts of the program, I improved upon it greatly. It should theoretically be able to operate on any file less than 2 gigabytes even though the program itself never accesses more than 16 bytes at one time.
I may have just invented the world’s smallest and fastest DOS hex dumper and editor. The official gitlab repository has the source code seen in this video as well as the Linux 32 bit Assembly and the original C version I wrote first when I invented this program.
I will have to make more videos showing examples of how it can be used, but I have a readme file in the repository that explains what it does and why I made it.
——–D-2142——————————- INT 21 – DOS 2+ – “LSEEK” – SET CURRENT FILE POSITION AH = 42h AL = origin of move 00h start of file 01h current file position 02h end of file BX = file handle CX:DX = (signed) offset from origin of new file position Return: CF clear if successful DX:AX = new file position in bytes from start of file CF set on error AX = error code (01h,06h) (see #01680 at AH=59h/BX=0000h) Notes: for origins 01h and 02h, the pointer may be positioned before the start of the file; no error is returned in that case (except under Windows NT), but subsequent attempts at I/O will produce errors if the new position is beyond the current end of file, the file will be extended by the next write (see AH=40h); for FAT32 drives, the file must have been opened with AX=6C00h with the “extended size” flag in order to expand the file beyond 2GB BUG: using this method to grow a file from zero bytes to a very large size can corrupt the FAT in some versions of DOS; the file should first be grown from zero to one byte and then to the desired large size SeeAlso: AH=24h,INT 2F/AX=1228h
Welcome to Chastity’s Hex Compare program also known as “chastecmp”.
Enter two filenames as command line arguments such as:
./chastecmp file1.txt file2.txt
It works for any binary files too, not just text. In fact for text comparison you want entirely different tools.
This tool can be used to find the tiny differences between files in hexadecimal. It shows only those bytes which are different. I wrote it as a solution to a reddit user who asked how to compare two files in hexadecimal.
It is an improvement over the Linux “cmp” tool which displays the offsets in decimal and the bytes in octal. Aside from using two different bases in the data, it falls short of usefulness because there are more hex editors than octal editors.
Here are also some graphical tools I can recommend if you are looking for a GUI instead of my command line program.
vbindiff
wxHexeditor
Below is the full source code:
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
int argx,x;
FILE* fp[3]; /*file pointers*/
int c1,c2;
long flength[3]; /*length of the file opened*/
/*printf("argc=%i\n",argc);*/
if(argc<3)
{
printf("Welcome to Chastity's Hex Compare program also known as \"chastecmp\".\n\n");
printf("Enter two filenames as command line arguments such as:\n");
printf("%s file1.txt file2.txt\n",argv[0]);
return 0;
}
argx=1;
while(argx<3)
{
fp[argx] = fopen(argv[argx], "rb"); /*Try to open the file.*/
if(!fp[argx]) /*If the pointer is NULL then this becomes true and the file open has failed!*/
{
printf("Error: Cannot open file \"%s\": ",argv[argx]);
printf("No such file or directory\n");
return 1;
}
/*printf("File \"%s\": opened.\n",argv[argx]);*/
printf("fp[%X] = fopen(%s, \"rb\");\n",argx,argv[argx]);
argx++;
}
printf("Comparing files %s and %s\n",argv[1],argv[2]);
argx=1;
while(argx<3)
{
fseek(fp[argx],0,SEEK_END); /*go to end of file*/
flength[argx]=ftell(fp[argx]); /*get position of the file*/
printf("length of file fp[%X]=%lX\n",argx,flength[argx]);
fseek(fp[argx],0,SEEK_SET); /*go back to the beginning*/
argx++;
}
x=0;
while(x<flength[1])
{
c1 = fgetc(fp[1]);
c2 = fgetc(fp[2]);
if(c1!=c2)
{
printf("%08X: %02X %02X\n",x,c1,c2);
}
x++;
}
argx=1;
while(argx<3)
{
fclose(fp[argx]);
printf("fclose(fp[%X]);\n",argx);
argx++;
}
return 0;
}
I have created two functions in x86 Assembly Language that allow me to output any zero terminated string or output any integer in any base from 2 to 36. I will show the full source so people can play with it. This uses system calls on Linux so it won’t work on Windows.
first, the header file “chaste-lib.asm”
; This file is where I keep my function definitions.
; These are usually my string and integer output routines.
putstring: ; function to print zero terminated string pointed to by register eax
mov edx,eax ; copy eax to edx as well. Now both registers have the address of the main_string
strlen_start: ; this loop finds the lenge of the string as part of the putstring function
cmp [edx],byte 0 ; compare byte at address edx with 0
jz strlen_end ; if comparison was zero, jump to loop end because we have found the length
inc edx
jmp strlen_start
strlen_end:
sub edx,eax ; edx will now have correct number of bytes when we use it for the system write call
mov ecx,eax ; copy eax to ecx which must contain address of string to write
mov eax, 4 ; invoke SYS_WRITE (kernel opcode 4)
;mov ebx, 1 ; ebx=1 means write to the STDOUT file
int 80h ; system call to write the message
ret ; this is the end of the putstring function return to calling location
radix dd 16 ;radix or base for integer output. 2=binary, 16=hexadecimal, 10=decimal
putint: ; function to output decimal form of whatever integer is in eax
push eax ;save eax on the stack to restore later
mov ebp,int_string+31 ;address of start digits
digits_start:
mov edx,0;
mov esi,[radix] ;radix is from memory location just before this function
div esi
cmp edx,10
jb decimal_digit
jge hexadecimal_digit
decimal_digit: ;we go here if it is only a digit 0 to 9
add edx,'0'
jmp save_digit
hexadecimal_digit:
sub edx,10
add edx,'A'
save_digit:
mov [ebp],dl
cmp eax,0
jz digits_end
dec ebp
jmp digits_start
digits_end:
mov eax,ebp ; now that the digits have been written to the string, display it!
call putstring
pop eax ;load eax from the stack so it will be as it was before this function was called
ret
next, the main source that includes the header: “main.asm”
format ELF executable
entry main
include 'chaste-lib.asm'
main: ; the main function of our assembly function, just as if I were writing C.
; I can load any string address into eax and print it!
mov ebx, 1 ;ebx must be 1 to write to standard output
mov eax,msg
call putstring
mov eax,main_string ; move the address of main_string into eax register
call putstring
mov [radix],2 ; can choose radix for integer output!
mov eax,0
loop1:
call putint
inc eax
cmp eax,100h;
jnz loop1
mov eax, 1 ; invoke SYS_EXIT (kernel opcode 1)
mov ebx, 0 ; return 0 status on exit - 'No Errors'
int 80h
; this is where I keep my string variables
msg db 'Hello World!', 0Ah,0 ; assign msg variable with your message string
main_string db "This is Chastity's Assembly Language counting program!",0Ah,0
int_string db 32 dup '?',0Ah,0
; This Assembly source file has been formatted for the FASM assembler.
; The following 3 commands assemble, give executable permissions, and run the program
;
; fasm main.asm
; chmod +x main
; ./main
If you have the fasm compiler, you can assemble this source on any Linux distribution running on an intel CPU. Assembly is platform specific but it runs really fast and the process of programming with it is enjoyable for me.
I have been learning x86 assembly language. I know a little bit from years ago but now it is coming back to me. I formerly did 16 bit DOS hobby programs. This is 32 bit Linux programming in assembly using FASM as my assembler.
format ELF executable
entry main
main: ; the main function of our assembly function, just as if I were writing C.
; I can load any string address into eax and print it!
mov eax,msg
call putstring
mov eax,main_string ; move the address of main_string into eax register
call putstring
mov eax,0
loop1:
call putint
inc eax
cmp eax,16;
jnz loop1
mov eax, 1 ; invoke SYS_EXIT (kernel opcode 1)
mov ebx, 0 ; return 0 status on exit - 'No Errors'
int 80h
; this is where I keep my string variables
msg: db 'Hello World!', 0Ah,0 ; assign msg variable with your message string
main_string db 'This is the assembly counting program!',0Ah,0
int_string db 32 dup '?',0Ah,0
; this is where I keep my function definitions
putstring: ; function to print zero terminated string pointed to by register eax
mov edx,eax ; copy eax to edx as well. Now both registers have the address of the main_string
strlen_start: ; this loop finds the lenge of the string as part of the putstring function
cmp [edx],byte 0 ; compare byte at address edx with 0
jz strlen_end ; if comparison was zero, jump to loop end because we have found the length
inc edx
jmp strlen_start
strlen_end:
sub edx,eax ; edx will now have correct number of bytes when we use it for the system write call
mov ecx,eax ; copy eax to ecx which must contain address of string to write
mov eax, 4 ; invoke SYS_WRITE (kernel opcode 4)
mov ebx, 1 ; write to the STDOUT file
int 80h ; system call to write the message
ret ; this is the end of the putstring function return to calling location
putint: ; function to output decimal form of whatever integer is in eax
push eax ;save eax on the stack to restore later
mov ebx,int_string+31 ;address of start digits
digits_start:
mov edx,0;
mov esi,10
div esi
add edx,'0'
mov [ebx],dl
cmp eax,0
jz digits_end
dec ebx
jmp digits_start
digits_end:
mov eax,ebx ; now that the digits have been written to the string, display it!
call putstring
pop eax ;load eax from the stack so it will be as it was before this function was called
ret
; This Assembly source file has been formatted for the FASM assembler.
; The following 3 commands assemble, give executable permissions, and run the program
;
; fasm main.asm
; chmod +x main
; ./main
This program only works on computers with Intel x86 CPUs and running a Linux distribution. the “int 80h” instructions mean interrupt 128. For some reason, this calls the Linux kernel. I don’t know why it works this way but I do know enough assembly to write my own string and integer routines to replace printf since it is not available in pure assembly like I could in the C programming language.
The negative powers of two are what happens when you start at 1 and keep dividing by 2. Although normally fractional numbers of this sort can’t exist for the integer type, I can simulate it using an array of decimal digits and my pattern recognition to write the small program below which gives the correct digits.
/*negative powers of two*/
#include <stdio.h>
#include <stdlib.h>
int main()
{
/*most important variable: number of digits*/
int length=64;
char *a;
int alength=2,x,y,temp;
a=(char*)malloc(length*sizeof(*a));if(a==NULL){printf("Failed to create array a\n");return(1);}
x=0;
while(x<length)
{
a[x]=0;
x++;
}
a[0]=1;
while(alength<length)
{
printf("%i.",a[0]);
x=1;
while(x<alength)
{
printf("%d",a[x]);
x++;
}
printf("\n");
y=0;
x=0;
while(x<length)
{
if( (a[x]&1)==1 ){temp=5;}else{temp=0;}
a[x]>>=1;
a[x]+=y;
y=temp;
x++;
}
if(a[alength]>0){alength++;}
}
if(a!=NULL){free(a); a=NULL;}
return 0;
}
I know this post is not exactly Chess related but I am thinking of using this site to double as a way to share some of my computer programming code too. I also may be able to create mathematical data structures to represent the game of Chess in some way.
I am writing a book about Chess and so I may sometimes share cool programs like this that are heavily math related. My obsession with numbers is how I got my start in programming originally.
At first I forgot to specify the /usr prefix. I messed up my system and nothing was compiling because the default /usr/local was where the new install was whereas all the other libraries were installed in subdirectories of /usr.
Therefore I had to repead the command and then remove the sdl2-config file from the /usr/local/bin directory.
Then everything compiled as before, even my Tetris game and my new LGBT font library.
Technically I did not need the latest SDL2 version available but I wanted to learn how to install updates from source rather than only relying on the Debian repositories.
This is because I am considering making my own Linux distribution.
This post isn’t Chess related but it is still very nerdy. I have made 3 different SDL programs that all do the exact same thing. However they are written using different SDL versions over time as the API has changed. Therefore, SDL version 1,2, and 3 source code is provided as well as the commands to compile them!
SDL Version 1
#include <stdio.h>
#include <SDL.h>
int width=1280,height=720;
int loop=1;
SDL_Surface *surface;
SDL_Event e;
int main(int argc, char **argv)
{
if(SDL_Init(SDL_INIT_EVERYTHING)){return 1;}
SDL_putenv("SDL_VIDEO_WINDOW_POS=center");
SDL_WM_SetCaption("SDL1 Program",NULL);
surface=SDL_SetVideoMode(width,height,32,SDL_SWSURFACE);
if(surface==NULL){return 1;}
SDL_FillRect(surface,NULL,0xFF00FF);
while(loop)
{
while(SDL_PollEvent(&e))
{
if(e.type==SDL_QUIT){loop=0;}
if(e.type==SDL_KEYUP){if(e.key.keysym.sym==SDLK_ESCAPE){loop=0;}}
}
SDL_Flip(surface);
}
SDL_Quit();
return 0;
}
/*
SDL1 program that creates a window but nothing else.
Closes when user presses Esc or clicks the X of the window.
This is to test whether SDL1 programs can be compiled.
With the sdl-config script:
gcc -Wall -ansi -pedantic sdl1-test.c -o sdl1-test `sdl-config --cflags --libs` && ./sdl1-test
Without the sdl-config script:
gcc -Wall -ansi -pedantic sdl1-test.c -o sdl1-test -I/usr/include/SDL -D_GNU_SOURCE=1 -L/usr/lib/x86_64-linux-gnu -lSDL && ./sdl1-test
*/
SDL Version 2
#include <stdio.h>
#include <SDL.h>
int width=1280,height=720;
int loop=1;
SDL_Window *window;
SDL_Surface *surface;
SDL_Event e;
int main(int argc, char **argv)
{
if(SDL_Init(SDL_INIT_VIDEO))
{
printf( "SDL could not initialize! SDL_Error: %s\n",SDL_GetError());return -1;
}
window=SDL_CreateWindow("SDL2 Program",SDL_WINDOWPOS_CENTERED,SDL_WINDOWPOS_CENTERED,width,height,SDL_WINDOW_SHOWN );
if(window==NULL){printf( "Window could not be created! SDL_Error: %s\n", SDL_GetError() );return -1;}
surface = SDL_GetWindowSurface( window ); /*get surface for this window*/
SDL_FillRect(surface,NULL,0xFF00FF);
SDL_UpdateWindowSurface(window);
printf("SDL Program Compiled Correctly\n");
while(loop)
{
while(SDL_PollEvent(&e))
{
if(e.type == SDL_QUIT){loop=0;}
if(e.type == SDL_KEYUP)
{
if(e.key.keysym.sym==SDLK_ESCAPE){loop=0;}
}
}
}
SDL_DestroyWindow(window);
SDL_Quit();
return 0;
}
/*
This source file is an example to be included in Chastity's Code Cookbook. This example follows the SDL version 2 which works differently than the most up to date version (version 3 at this time). There is also an updated version in the same repository that works with version 3.
Linux Compile Command:
With the sdl2-config script:
gcc -Wall -ansi -pedantic sdl2-test.c -o sdl2-test `sdl2-config --cflags --libs` && ./sdl2-test
Without the sdl2-config script:
gcc -Wall -ansi -pedantic sdl2-test.c -o sdl2-test -I/usr/include/SDL2 -lSDL2 && ./sdl2-test
*/
SDL Version 3
#include <stdio.h>
#include <SDL.h>
int width=1280,height=720;
int loop=1;
SDL_Window *window;
SDL_Surface *surface;
SDL_Event e;
int main(int argc, char **argv)
{
if(!SDL_Init(SDL_INIT_VIDEO))
{
printf( "SDL could not initialize! SDL_Error: %s\n",SDL_GetError());return -1;
}
window=SDL_CreateWindow("SDL3 Program",width,height,0);
if(window==NULL){printf( "Window could not be created! SDL_Error: %s\n", SDL_GetError() );return -1;}
surface = SDL_GetWindowSurface( window ); /*get surface for this window*/
SDL_FillSurfaceRect(surface,NULL,0xFF00FF);
SDL_UpdateWindowSurface(window);
printf("SDL Program Compiled Correctly\n");
while(loop)
{
while(SDL_PollEvent(&e))
{
if(e.type == SDL_EVENT_QUIT){loop=0;}
if(e.type == SDL_EVENT_KEY_UP)
{
if(e.key.key==SDLK_ESCAPE){loop=0;}
}
}
}
SDL_DestroyWindow(window);
SDL_Quit();
return 0;
}
/*
This source file is an example to be included in Chastity's Code Cookbook. By following the migration guide, I converted the SDL2-test program to the changes in SDL3.
https://wiki.libsdl.org/SDL3/README-migration
Windows Compile Command:
gcc -Wall -ansi -pedantic sdl3-test.c -o sdl3-test -IC:/w64devkit/include/SDL3 -lSDL3 && sdl3-test
Linux Compile Command:
gcc -Wall -ansi -pedantic sdl3-test.c -o sdl3-test -I/usr/include/SDL3 -lSDL3 && ./sdl3-test
*/
No matter which of those I compile and run, I get the same result:
All the programs do is create a window and fill the whole thing with Magenta. It’s not that exciting, but it is pretty, and it shows that all 3 versions of SDL work on my Debian Linux machine.
This is an example of custom written HTML for a table representing a Chessboard. This version does not use CSS and instead styles every single element individually. The reason is because I am unable to control the CSS of WordPress the way I want. This should work for embedding a table of a Chessboard into any WordPress post I want. Creating an HTML table is an idea I had to save disk space if I were to make a personal database of Chess positions and my thoughts on various moves.
There are other ways to make a text form of a Chess board. For example, consider this table made using Markdown:
a
b
c
d
e
f
g
h
8
r
n
b
q
k
b
n
r
7
p
p
p
p
p
p
p
p
6
5
4
3
2
P
P
P
P
P
P
P
P
1
R
N
B
Q
K
B
N
R
The Markdown code that makes the above table is:
||a|b|c|d|e|f|g|h|
|:---:|:---:|:---:|:---:|:---:|:---:|:---:|:---:|:---:|
| 8 | r | n | b | q | k | b | n | r |
| 7 | p | p | p | p | p | p | p | p |
| 6 | | | | | | | | |
| 5 | | | | | | | | |
| 4 | | | | | | | | |
| 3 | | | | | | | | |
| 2 | P | P | P | P | P | P | P | P |
| 1 | R | N | B | Q | K | B | N | R |
However, the Markdown version, although it displays fine when viewed on Github, does not play well with WordPress. Therefore, the HTML version at the top of this post is much more reliable for WordPress posts.
Reading the code on the other hand is another story! The following HTML code made the table at the very top of this post.
When considering the difficulty of getting a workable table to display on my blog, you may wonder why I bother? Consider the following image which is the equivalent starting position of Chess.
Obviously, this image looks better than the lame table at the top of this post, but it comes with a few downsides.
First, the amount of time it takes to load up inkscape, move the pieces around for the image, choose a file name to save as, upload the file to my WordPress media account, and then write a Markdown link to the image is far more than the time it takes to edit the HTML code to mode the pieces of text around.
Second, linking to image files adds the dependency of external files existing and the internet connection working. By keeping the table as plain text, it allows me to work offline and save time and space in the creation of the image and my explanations of that position.
Therefore, I have decided to use the HTML table method for some time as I build content for my next Chess book. However, it will not be done any time soon. The first book was for beginners, but the next book will hopefully be for the more advanced and skilled player.
What you see above is an HTML table representation of a Chess board. It is not perfect, but it is a convenient way to avoid the use of images when creating documentation on different Chess positions. Using plain text saves a lot of space compared to the space required to make millions of Chess images for every possible move. The white pieces are represented with capital letters and the black by lowercase letters.
Of course, I don’t intend to document every move that could occur in every Chess game. Instead, I want to cover those that happen in real games with humans. The idea is approximately one blog post per day where I analyze the board from my human perspective and then write what I think the best move is.
Unlike a Chess engine, I can explain the reason for my moves in a position. This could be used for future Chess books after I have written good explanations of why I recommend a move in a given position.
In any case, my recommended move from the starting position will always be d4, which means move the pawn in front of the white queen to d4.
Later on, I may cover openings starting with e4, but because they are too popular and overused, I will be sticking with my openings beginning with d4. The idea is one post per day because this is a small step that is easy to fit into my busy schedule.
Unfortunately, my table does not display on WordPress the way it does on other sites like Github. I am working on ways to resolve this with HTML or CSS but my needs are very specific and this may take time.
Today’s post is not exactly Chess related other than the fact that this checkered dress I bought 7 years ago was purchased because I am obsessed with Chess and the pattern of the Chess board. My skills are not only limited to playing Chess and writing books. I also like to dance to all kinds of music. Enjoy this silly video of me dancing to “I Will Survive” by Gloria Gaynor.