Code Example

I was doing some thinking the last month or so about how I didnt appreciate the tools and freedoms that Half-Life 2 provided me with. Something about working in CPP made my abilites as a coder quite free and open. I felt so much more challenged and thus motivated to push through and achieve things that were seemingly impossible. 

This last week we had a challenge, write an application that uses as few variables as possible in MASM Assembly that is able to calculate the day of the week that we were born, and as such, allow us to calculate the day of the week that any date provided is. 

Below you will find my solution…

TITLE Find The Day        (Lab4.asm)

; Description: Calculate the day of the week for a given day (Not using calculus)
; Author: Bob Chatman
; Revision Date: 10/23/08

INCLUDE Irvine32.inc

.data
prompts BYTE "Year: ", 0, "Month: ", 0, "Day: ", 0, 0dh, 0ah, "Day", 0dh, 0ah, "0: Sunday", 0dh, 0ah, "1: Monday", 0dh, 0ah, "2: Tuesday", 0dh, 0ah, "3: Wednesday", 0dh, 0ah, "4: Thursday", 0dh, 0ah, "5: Friday", 0dh, 0ah, "6: Saturday", 0dh, 0ah, "The day you entered is day: "
m WORD 0

.code
main PROC
    call Clrscr

    ; Prompt for year
    mov edx, OFFSET prompts
    call WriteString

    ; Read in year and move it into CX
    call ReadDec
    mov cx, ax			; Year is in CX						2008

    ; Prompt for month
    mov  edx, OFFSET prompts + 7
    call WriteString

    ; Read in month and store it in m
    call ReadDec
    mov m, ax			; Month stored in m					10

    ; Prepare and calculate "a" value
    neg ax				; -Month							-10
    add ax, 14			; -Month + 14						4
    mov dx, 0			; Clear dx register
    mov bx, 12			; bx=12
    div bx				; ax/12 == (-Month + 14) / 12 = a	0 r 4

    ; Prepare and calculate "y" value
    sub cx, ax			; year - a = y						2008

    ; prepare and calculate the d value, dont bother storing it
    mul bx				; 12 * a							0
    sub ax, 2			; 12 * a - 2						-2
    add ax, m			; 12 * a - 2 + Month				8
    mov bx, ax			; move to bx register
    mov ax, 31
    mul bx				; 31 * m							31 * 8 = 248
    mov bx, 12
    div bx				; (31 * m) / 12						248/12 = 20 r 8
    mov bx, ax			; move to bx register
    mov ax, cx			; ax = y
    add cx, bx			; y + (31 * m) / 12					2008 + 20 = 2028
    mov bx, 4
    mov dx, 0			; clear out dx register
    div bx				; y / 4								ax = 502
    add cx, ax			; y + (31 * m) / 12 + y / 4			cx = 2028 + 502 =
    mov bx, 25
    mov dx, 0			; clear out dx register
    div bx				; y / 100
    sub cx, ax			; y + (31 * m) / 12 + y / 4 - y / 100
    mov bx, 4
    mov dx, 0			; clear out dx register
    div bx				; y / 400
    add cx, ax			; y + (31 * m) / 12 + y / 4 + y / 100 + y / 400

    ; Prompt for day
    mov  edx, OFFSET prompts + 15    ; Write Month Prompt
    call WriteString

    ; Read in day
    call ReadDec
    add ax, cx			; y + (31 * m) / 12 + y / 4 + y / 100 + y / 400 + day
    mov bx, 7
    mov dx, 0			; clear out dx register
    idiv bx
    mov ax, dx			; get remainder out of dx

    ; Write listing of days and decimal value
    mov edx, OFFSET prompts + 21 ; Index listing
    call WriteString
    call WriteDec

    exit
main ENDP

END main