Tag: technology

  • Chapter 9: Bitwise Operations for Advanced Nerds

    Today’s post is a preview of chapter 9 in my upcoming book on programming in DOS. If anything, this chapter is more of a joke or a meme than actually useful for writing most programs. I was feeling naughty and decided to show how far down the coding rabbit hole I have traveled!

    This chapter contains information which will assist you in understanding more about how computers work, but that in general is not required for MOST programming unless you are trying to operate on individual bits.

    To start out, I will describe 5 essential bitwise operations independently of any specific programming language. This is because these operations exist in every programming language I know of, including Assembly and C.

    After I have explained what the bitwise operations do, I will give examples of how this can be used in Assembly language to substitute for addition and subtraction! You might wonder why you would do this, the fact is that you don’t need to but it is a fun trick that only advanced nerds like me do for a special challenge.

    The Bitwise Operations

    This chapter explains 5 bitwise operations which operate on the bits of data in a computer. For the purpose of demonstration, it doesn’t matter which number the bits represent at the moment. This is because the bits don’t have to represent numbers at all but can represent anything described in two states. Bits are commonly used to represent statements that are true or false. For the purposes of this section, the words AND, OR, XOR are in capital letters because their meaning is only loosely related to the English words they get their name from.

    Bitwise AND Operation

    0 AND 0 == 0
    0 AND 1 == 0	
    1 AND 0 == 0
    1 AND 1 == 1
    

    Think of the bitwise AND operation as multiplication of single bits. 1 times 1 is always 1 but 0 times anything is always 0. That’s how I personally think of it. I guess you could say that something is true only if two conditions are true. For example, if I go to Walmart AND do my job then it is true that I get paid.

    I like to think of the AND operation as the “prefer 0” operation. It will always choose a 0 if either of the two bits is a 0, otherwise, if no 0 is available, it will choose 1.

    Bitwise OR Operation

    0 OR 0 == 0
    0 OR 1 == 1	
    1 OR 0 == 1
    1 OR 1 == 1
    

    The bitwise OR operation can be thought of as something that is true if one or two conditions are true. For example, it is true that playing in the street will result in you dying because you got run over by a car. It is also true that if you live long enough, something else will kill you. Therefore, the bit of your impending death is always 1.

    I like to think of the OR operation as the “prefer 1” operation. It will always choose a 1 if one of the two bits is a 1, otherwise, if no 1 is available, it will choose 0.

    Bitwise XOR Operation

    0 XOR 0 == 0
    0 XOR 1 == 1	
    1 XOR 0 == 1
    1 XOR 1 == 0
    

    The bitwise XOR operation is different because it isn’t really used much for evaluating true or false. Instead, this operation returns 1 if the bits compared are different or 0 if they are the same. This means that any bit, or group of bits, XORed with itself, will always result in 0.

    If you look at my XOR chart above, you will see that using XOR of any bit with a 1 causes the result to be the opposite of the original bit.

    The XOR operation is the quickest way to achieve this bit inversion. If you have a setting that you want to switch on or off, you can toggle it by XORing that bit with 1.

    While the AND, OR, XOR operations can work in the context of individual bits, or groups of them, the next operations, the bit shifts, only make sense in the context of a group of bits. At minimum, you will be operating on 8 bits at a time because a byte is the lowest addressable size of memory.

    Bitwise Left and Right Shift Operations

    Consider the case of the following 8 bit binary value:

    00001000

    This would of course represent the number 8 because a 1 is in the 8’s place value. We can left shift or right shift.

    00001000 ==  8 : is the original byte
    
    00010000 == 16 : original left shift 1
    00000100 ==  4 : original right shift 1
    

    That is really all there is to shifts. They can be used to multiply or divide by a power of two. In some cases, this can be faster than using the mul and div instructions described in chapter 4.

    Example 0: Fake Add

    The following example shows how it is possible to write an addition routine using a combination of the AND,XOR,SHL operations. In this case, the numbers are shown in decimal to be easier for most people to see that the addition is correct.

    org 100h
    
    main:
    
    mov word [radix],10 ; choose radix for integer input/output
    mov word [int_width],1
    
    mov di,1987
    mov si,38
    
    mov ax,di
    call putint
    mov ax,si
    call putint
    call putline
    
    fake_add:
    mov ax,di
    xor di,si
    and si,ax
    shl si,1
    jnz fake_add
    
    mov ax,di
    call putint
    mov ax,si
    call putint
    
    mov ax,4C00h
    int 21h
    
    include 'chastelib16.asm'
    

    If you run it, you will see that the correct result of 2025 which is 1987+38. These are the values we set the di and si registers to before simulating addition with these fancy bitwise operations that make even seasoned programmers run scared.

    But how does this monstrosity of a program work? You see the AND operation keeps track of whether both bits in each place value are 1 or not. If they both are, this means that we have to “carry” those bits as we would do in an ordinary binary division. We store the carry in the si register and then left shift it once each time in the loop. The loop continues until si equals zero and there are no more bits to invert with XOR.

    The fact that it works is easy to work out in my head but I don’t blame you if you can’t visualize it. However, this shows the power of what bit operations can do, even though you will probably never need to do this.

    Example 1: Fake Sub

    In case the fake addition example above wasn’t enough for you, here is a slightly modified example that does a fake subtraction operation using the same operations. Try it out and you will see that it subtracts 38 from 2025 and gets the original 1987.

    org 100h
    
    main:
    
    mov word [radix],10 ; choose radix for integer input/output
    mov word [int_width],1
    
    mov di,2025
    mov si,38
    
    mov ax,di
    call putint
    mov ax,si
    call putint
    call putline
    
    fake_sub:
    xor di,si
    and si,di
    shl si,1
    jnz fake_sub
    
    mov ax,di
    call putint
    mov ax,si
    call putint
    
    mov ax,4C00h
    int 21h
    
    include 'chastelib16.asm'
    

    I will try to explain how this works. You see, we first XOR the di register with the si register. Then, we AND si with the new value of di. This means that the bits in the current place value will only both be 1 if those bits were 0 in di and then were inverted to 1 by the XOR with si. This means that at the start of the loop, destination bit=0 and source bit=1. 0 minus 1 means that we need to “borrow” (I hate that term because it is really stealing because we never give it back). We left shift si as usual and then we keep XORing the new borrow in si until it is zero.

    Also, you may have noticed that I never used the “cmp” instruction to compare si with zero in this examples. This is because the zero flag is automatically updated with most operations. In fact there are places in my standard library of functions (chastelib) where it wasn’t strictly required to compare with “cmp” but I added it for clarity so I could read my code and more easily remember what I was doing.

    But let’s face it, the examples in this chapter are purely for showing off how advanced my knowledge of the binary numeral system and manipulating bits in ways no reasonable person should ever attempt. I must admit, it would be great for an obfuscated code contest to make a program with code that is unreadable to most humans.

  • Chapter 10: Software Licenses

    This blog has turned into most of my rants about computer programming recently, but I still play and teach Chess in case you are interested. But tonight, I spent some time writing another chapter of my programming book, Chastity’s Code Cookbook.

    Chapter 10: Software Licenses

    Perhaps it could be said that once you have written a program, what you do with it is even more important. For most of my life, I never considered the concept of copyright or ownership of the toy programs I wrote. I figured that unless I made a great game or operating system that I would not need to consider writing the terms and conditions about what people can or should do with my work.

    And even now, I don’t think my programs have enough of an impact for anyone to care about software licenses. However, I have found some software licenses that are compatible with my personal philosophy for how software should be shared and distributed.

    Generally, I only recommend software that is considered “Free Software” by the definitions of the Free Software Foundation.

    I have been an advocate of Free Software as it is directly tied to Freedom of speech. I am well aware that Software Freedom and Open source are usually, but not always, the same thing. This page by the FSF on the GNU project links to licenses where you can read the full text. However, I will also provide my summaries based on my understanding.

    https://www.gnu.org/licenses/license-list.html

    GNU GENERAL PUBLIC LICENSE Version 3

    https://www.gnu.org/licenses/gpl-3.0.html

    This section is the abridged and simplified version by Chastity White Rose. In case of confusion, see the original text.

    The General Public License guarantees your Freedom to change a program licensed under it and to share it with others. However, when you share it with others, they must have the same Freedom you do. Therefore, you must give others access to your source code if you choose to distribute your own modified version.

    Part of this Freedom is to include the source code when you distribute it. Source code is the preferred form of the program that makes it possible and/or easy to modify, provided you know the programming language being used. Examples include source files for languages such as C, C++, Assembly, Pascal, or Java. Also included in this definition are build scripts written in Bash, Windows Batch, GNU Make, or any similar system.

    In my opinion, the benefit of the GPL3, as well as past and future versions of it, is that it declares the author does not intend it to be used in proprietary programs. As a programmer and author, I would not want a big tech company to come along and use my code for evil purposes or to restrict others from accessing what I intended to be free.

    At the time of this writing, I don’t think anything I have written is in danger of being misused. Still, I hope that people use my programs if they find them useful, modify them to make them more useful to their purposes, and share their work with others who go on to do the same.

    The reason for restricting proprietary use is so that another person or company can’t take my code, claim to be the original owner, and turn it into something opposed to what I intended. More importantly, I never want someone to charge money for my software. Specifically in the case of software written by me, Chastity White Rose, Free Software is free as in Free Speech and Free Price.

    Considering all this, I place all my code in this book under the GPL3 license so that the Free Software Foundation and entire world of Open Source and Free Software nerds can take action on my behalf if someone ever tries to restrict my work after my death. These words are my statement that you can’t steal what was made to be free.

    GNU Lesser General Public License Version 3

    https://www.gnu.org/licenses/lgpl-3.0.html

    The Lesser GPL is very much like the regular GPL with an important exception. It is to allow proprietary programs to use a library. At first, I didn’t see the point of this; however, when I read the following article, I came to understand better.

    https://www.gnu.org/licenses/why-not-lgpl.html

    The reason it was to the advantage of the Free Software community to allow the GNU C library to be linked by proprietary programs is that it prevents the need for those developers to rely on other proprietary software.

    When I think about it, would I really want someone to have to rely on a C compiler or library made by Microsoft or Apple because they couldn’t use GNU Libc for the proprietary game they made? No, I wouldn’t want that. I will explain my reasons for this.

    Basically, complying with the normal GPL3 prevents someone from profiting from their work. If you are required to provide the source code, then anyone smart enough to compile it on their system can copy and modify your game infinitely without paying you.

    The Lesser GPL3 allows someone, for example, to use a free library to implement the graphics, sound, etc., for their game or utility program without providing the source code to their own program, which uses these libraries, but does not copy code from it.

    In short, if you want to make money, you probably want the Lesser GPL, but if you just want to be a nice person and make your code free for the education and entertainment of all people without promise of reward, the normal GPL serves the purpose best in my opinion.

    There are hundreds of other software licenses to be considered if you have written a program and want to decide which terms to release it under. You can, of course, create your own from scratch, but it might be worth reading about those that already exist to see if they match your ideals.

    Free vs. Proprietary

    I am not against proprietary programs when they are video games for entertainment only. I also believe the programmers should be paid for their work, like any other job, to help them survive.

    But my personal ideals are different from those of most programmers. My goal is to write books to share my code and to teach people how to do things, but I don’t plan to profit off the code because I value individual Freedom more than I do money. I hope that even after my death, others will still learn the joy of computer programming and use it to make great things.

    Writing computer software isn’t just a hobby or a business; it is a ministry. Think about all the software that has been written to create the internet and allow people to write books and share them with the world. Think about the programmers who made video and audio recording, encoding, and formatting possible.

    This book is about computer programming, not religion, but I must say, if you had a message that would save the lives or the souls of others, would you really want to be restricted in what manner you use to share that information? Therefore, I propose that Free Software is a necessity in light of Digital Rights Management and companies like Amazon removing ebooks from people’s devices that they have already paid for.

    Traditional books are dying, and bookstores are closing. If we don’t work together to stop digital book burning, then we lose the final method of sharing words of eternal value.

  • chastehex-DOS-32-bit

    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.

    https://gitlab.com/chastitywhiterose/chastehex.git

    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

  • DOS Assembly putstring function

    This is a small program which uses the putstring function I wrote. This function is one of 4 ultimate functions I have created which make up “chastelib”. DOS programming is simpler than Windows and is not that different from Linux in that system calls are done with an interrupt.

    org 100h
    
    main:
    
    mov ax,text
    call putstring
    
    mov ax,4C00h
    int 21h
    
    text db 'Hello World!',0Dh,0Ah,0
    
    ;This section is for the putstring function I wrote.
    ;It will print any zero terminated string that register ax points to
    
    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
    

    Anyone can assemble and run this source code, but you will need a DOS emulator like DOSBox in order for it to work. In fact, I have a video showing me assembling and running it inside of DOSBox.

    Lately I have been having a programming phase and am working on a book about programming in DOS. There is no money involved in this because nobody except nerds like me care about DOS. Speaking of nerds, if you follow my blog, don’t forget that this site was set up for teaching Chess. Leave me a comment if you play Chess online or live in Lee’s Summit. I am still playing Chess every day although some of my time has been taken up with programming in Assembly language because it is so much fun.

    If you like this post, you may be interested in my much longer post/book that is all about Assembly programming in DOS.

  • Assembly Language Counting Program

    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.