Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Sample Programs #3

Open
jere-mie opened this issue Jan 9, 2023 · 17 comments
Open

Add Sample Programs #3

jere-mie opened this issue Jan 9, 2023 · 17 comments

Comments

@jere-mie
Copy link
Owner

jere-mie commented Jan 9, 2023

It would be awesome to have some sample programs added to this repository so students can have an easier time learning MASM. I haven't actually coded in MASM for a long time though so I'm not well suited to work on this.

@jere-mie
Copy link
Owner Author

chatgpt might be helpful for this....

@jere-mie
Copy link
Owner Author

Update: it certainly is helpful

@jere-mie
Copy link
Owner Author

TITLE Input and Output

INCLUDE Irvine32.inc

.data
    prompt BYTE "Enter a number: ", 0
    resultMsg BYTE "You entered: ", 0
    inputBuffer BYTE 16 DUP(0)
    resultBuffer BYTE 16 DUP(0)

.code
main PROC
    mov edx, OFFSET prompt          ; Load prompt into edx
    call WriteString                ; Write the prompt to the console
    call ReadString                 ; Read the input from the console
    mov edx, OFFSET resultMsg       ; Load result message into edx
    call WriteString                ; Write the result message to the console
    mov eax, OFFSET inputBuffer     ; Load the address of the input buffer into eax
    mov edx, OFFSET resultBuffer    ; Load the address of the result buffer into edx
    call StrCopy                    ; Copy the input to the result buffer
    call WriteString                ; Write the result to the console
    call Crlf                       ; Add a newline to the console
    exit                            ; Exit the program

main ENDP
END main

@jere-mie
Copy link
Owner Author

TITLE Looping

INCLUDE Irvine32.inc

.code
main PROC
    mov eax, 0           ; Initialize counter to 0

    ; Loop 10 times
    L1:
    inc eax              ; Increment the counter
    cmp eax, 10          ; Check if we've reached 10
    jle L1               ; If not, repeat the loop

    call WriteInt        ; Write the final value of the counter (10) to the console
    call Crlf            ; Add a newline to the console
    exit                 ; Exit the program

main ENDP
END main

@jere-mie
Copy link
Owner Author

TITLE Simple Arithmetic

INCLUDE Irvine32.inc

.code
main PROC
    mov eax, 10           ; Load 10 into eax
    mov ebx, 5            ; Load 5 into ebx
    add eax, ebx          ; Add eax and ebx
    call WriteInt         ; Write the result (15) to the console
    call Crlf             ; Add a newline to the console
    exit                  ; Exit the program

main ENDP
END main

@jere-mie
Copy link
Owner Author

TITLE Hello World

INCLUDE Irvine32.inc

.code
main PROC
    mov edx, OFFSET helloMsg   ; Load message into edx
    call WriteString           ; Write the message to the console
    call Crlf                  ; Add a newline to the console
    exit                       ; Exit the program

helloMsg BYTE "Hello, World!", 0

main ENDP
END main

@jere-mie
Copy link
Owner Author

TITLE Factorial Calculation

INCLUDE Irvine32.inc

.data
    prompt BYTE "Enter a positive integer: ", 0
    resultMsg BYTE "The factorial of ", 0
    resultMsg2 BYTE " is: ", 0
    inputBuffer BYTE 16 DUP(0)
    resultBuffer BYTE 16 DUP(0)

.code
main PROC
    mov edx, OFFSET prompt          ; Load prompt into edx
    call WriteString                ; Write the prompt to the console
    call ReadString                 ; Read the input from the console
    mov eax, inputBuffer            ; Load the input buffer into eax
    call StrToUInt                  ; Convert the input string to an unsigned integer
    mov ecx, eax                    ; Move the input value into ecx
    mov eax, 1                      ; Initialize the factorial value to 1

    ; Calculate the factorial of the input value
    L1:
    mul ecx
    loop L1

    mov edx, OFFSET resultMsg       ; Load result message into edx
    call WriteString                ; Write the result message to the console
    mov eax, inputBuffer            ; Load the input buffer into eax
    mov edx, OFFSET resultBuffer    ; Load the result buffer into edx
    call StrCopy                    ; Copy the input to the result buffer
    call WriteString                ; Write the input value to the console
    mov edx, OFFSET resultMsg2      ; Load result message 2 into edx
    call WriteString                ; Write the second result message to the console
    call WriteDec                   ; Write the factorial value to the console
    call Crlf                       ; Add a newline to the console
    exit                            ; Exit the program

main ENDP
END main

@jere-mie
Copy link
Owner Author

TITLE Bubble Sort

INCLUDE Irvine32.inc

.data
    arr DWORD 5, 2, 6, 1, 3          ; Array to sort
    msg BYTE "Before sorting: ", 0
    msg2 BYTE "After sorting: ", 0
    count DWORD LENGTHOF arr

.code
main PROC
    ; Print the unsorted array
    mov edx, OFFSET msg              ; Load the message into edx
    call WriteString                 ; Write the message to the console
    mov esi, OFFSET arr              ; Load the address of the array into esi
    mov ecx, count                   ; Load the number of elements into ecx
    L1:
    mov eax, [esi]                   ; Load the current element into eax
    call WriteInt                    ; Write the current element to the console
    add esi, TYPE arr                ; Increment esi to point to the next element
    loop L1                          ; Repeat for all elements
    call Crlf                        ; Add a newline to the console

    ; Sort the array using bubble sort
    mov edx, OFFSET arr              ; Load the address of the array into edx
    mov ecx, count - 1               ; Load the number of elements minus one into ecx
    L2:
    mov esi, edx                     ; Load esi with the address of the array
    add esi, TYPE arr                ; Increment esi to point to the next element
    mov ebx, ecx                     ; Load ebx with the number of elements minus one
    L3:
    cmp [edx], [esi]                 ; Compare the current element with the next element
    jle L4                           ; If the current element is less than or equal to the next element,
                                     ; jump to the next iteration
    ; If the current element is greater than the next element, swap them
    mov eax, [edx]
    mov ebx, [esi]
    mov [edx], ebx
    mov [esi], eax
    L4:
    add edx, TYPE arr                ; Increment edx to point to the next element
    add esi, TYPE arr                ; Increment esi to point to the next element
    dec ebx                          ; Decrement ebx (the number of elements remaining)
    jnz L3                           ; If there are still elements remaining, repeat the loop

    ; Print the sorted array
    mov edx, OFFSET msg2             ; Load the message into edx
    call WriteString                 ; Write the message to the console
    mov esi, OFFSET arr              ; Load the address of the array into esi
    mov ecx, count                   ; Load the number of elements into ecx
    L5:
    mov eax, [esi]                   ; Load the current element into eax
    call WriteInt                    ; Write the current element to the console
    add esi, TYPE arr                ; Increment esi to point to the next element
    loop L5                          ; Repeat for all elements
    call Crlf                        ; Add a newline to the console

    exit                            ; Exit the program
main ENDP
END main

@jere-mie
Copy link
Owner Author

TITLE Sum of Array

INCLUDE Irvine32.inc

.data
    arr DWORD 5, 2, 6, 1, 3          ; Array to calculate the sum of
    count DWORD LENGTHOF arr
    sum DWORD ?

.code
main PROC
    ; Calculate the sum of the array
    mov esi, OFFSET arr              ; Load the address of the array into esi
    mov ecx, count                   ; Load the number of elements into ecx
    xor eax, eax                     ; Clear eax to 0
    L1:
    add eax, [esi]                   ; Add the current element to eax
    add esi, TYPE arr                ; Increment esi to point to the next element
    loop L1                          ; Repeat for all elements
    mov sum, eax                     ; Move the sum to the sum variable

    ; Print the sum of the array
    mov edx, OFFSET arr              ; Load the address of the array into edx
    call WriteArray                  ; Write the array to the console
    mov edx, OFFSET sum              ; Load the sum into edx
    call WriteString                 ; Write the message to the console
    call WriteInt                    ; Write the sum to the console
    call Crlf                        ; Add a newline to the console

    exit                            ; Exit the program
main ENDP
END main

@jere-mie
Copy link
Owner Author

TITLE Reverse String

INCLUDE Irvine32.inc

.data
    buffer BYTE 256                 ; Buffer to hold the string
    prompt BYTE "Enter a string: ", 0
    reverseMsg BYTE "Reversed string: ", 0

.code
main PROC
    ; Read the string from the user
    mov edx, OFFSET prompt
    call WriteString
    mov edx, OFFSET buffer
    mov ecx, LENGTHOF buffer
    call ReadString

    ; Reverse the string
    mov esi, OFFSET buffer         ; Load the address of the buffer into esi
    mov edi, esi                   ; Set edi to the same address as esi
    add edi, LENGTHOF buffer - 2   ; Set edi to the last character of the buffer
    mov ecx, LENGTHOF buffer - 1   ; Set ecx to the length of the string
    shr ecx, 1                     ; Divide ecx by 2
    L1:
    mov al, [esi]                  ; Load the current character into al
    mov bl, [edi]                  ; Load the character at the opposite end of the string into bl
    mov [esi], bl                  ; Copy bl to the current position in the string
    mov [edi], al                  ; Copy al to the opposite end of the string
    inc esi                        ; Increment esi to move forward in the string
    dec edi                        ; Decrement edi to move backward in the string
    loop L1                        ; Repeat for half the length of the string

    ; Print the reversed string
    mov edx, OFFSET reverseMsg
    call WriteString
    mov edx, OFFSET buffer
    call WriteString
    call Crlf

    exit                          ; Exit the program
main ENDP
END main

@jere-mie
Copy link
Owner Author

TITLE Factorial Calculator

INCLUDE Irvine32.inc

.data
    prompt BYTE "Enter a number: ", 0
    resultMsg BYTE "Factorial of ", 0
    errorMsg BYTE "Invalid input. Please enter a positive integer.", 0
    buffer BYTE 11                  ; Buffer to hold the user's input
    num DWORD ?
    result DWORD ?

.code
main PROC
    ; Get the user's input
    mov edx, OFFSET prompt
    call WriteString
    mov edx, OFFSET buffer
    mov ecx, LENGTHOF buffer
    call ReadString
    call ParseInt
    mov num, eax

    ; Calculate the factorial
    mov eax, num                   ; Load the number to calculate the factorial of into eax
    mov ecx, eax                   ; Copy eax to ecx
    dec ecx                        ; Decrement ecx to start the loop at num - 1
    L1:
    mul ecx                        ; Multiply eax by ecx
    loop L1                        ; Decrement ecx and repeat until ecx = 0
    mov result, eax                ; Move the result to the result variable

    ; Print the result
    cmp num, 0                     ; Check if the user entered a valid number
    jle error                      ; Jump to error if the number is less than or equal to 0
    mov edx, OFFSET resultMsg
    call WriteString
    mov edx, num
    call WriteInt
    mov edx, OFFSET buffer
    mov ecx, LENGTHOF buffer
    call WriteString
    mov edx, OFFSET result
    call WriteInt
    call Crlf
    jmp done                       ; Jump to the end of the program
    error:
    mov edx, OFFSET errorMsg
    call WriteString
    call Crlf
    done:
    exit

main ENDP
END main

@jere-mie
Copy link
Owner Author

TITLE Prime Number Checker

INCLUDE Irvine32.inc

.data
    prompt BYTE "Enter an integer: ", 0
    primeMsg BYTE " is a prime number.", 0
    notPrimeMsg BYTE " is not a prime number.", 0
    buffer BYTE 11                  ; Buffer to hold the user's input
    num DWORD ?
    divisor DWORD 2

.code
main PROC
    ; Get the user's input
    mov edx, OFFSET prompt
    call WriteString
    mov edx, OFFSET buffer
    mov ecx, LENGTHOF buffer
    call ReadString
    call ParseInt
    mov num, eax

    ; Check if the number is prime
    mov eax, num                   ; Load the number to check into eax
    mov edx, 0                     ; Set edx to 0
    mov ecx, divisor               ; Set ecx to 2
    L1:
    cmp edx, 0                     ; Check if edx is 0
    jne notPrime                   ; Jump to notPrime if edx is not 0
    cmp ecx, eax                   ; Check if ecx is equal to the number being checked
    je isPrime                     ; Jump to isPrime if ecx is equal to the number being checked
    mov edx, 0                     ; Set edx to 0
    mov ebx, eax                   ; Load eax into ebx
    div ecx                        ; Divide ebx by ecx
    cmp edx, 0                     ; Check if the remainder is 0
    jne L2                         ; Jump to L2 if the remainder is not 0
    mov edx, 1                     ; Set edx to 1 if the remainder is 0
    notPrime:
    inc ecx                        ; Increment ecx
    jmp L1                         ; Repeat the loop
    isPrime:
    mov edx, OFFSET primeMsg
    jmp printResult                ; Jump to printResult
    L2:
    jmp notPrime                   ; Jump to notPrime

    printResult:
    mov ecx, LENGTHOF buffer
    mov edx, OFFSET buffer
    call WriteString
    mov edx, num
    call WriteInt
    mov edx, OFFSET (primeMsg - 1)
    call WriteString
    call Crlf
    exit

main ENDP
END main

@jere-mie
Copy link
Owner Author

TITLE Factorial Calculator

INCLUDE Irvine32.inc

.data
    prompt BYTE "Enter a positive integer: ", 0
    factMsg BYTE "The factorial of ", 0
    isMsg BYTE " is ", 0
    buffer BYTE 11              ; Buffer to hold the user's input
    num DWORD ?
    fact DWORD 1

.code
main PROC
    ; Get the user's input
    mov edx, OFFSET prompt
    call WriteString
    mov edx, OFFSET buffer
    mov ecx, LENGTHOF buffer
    call ReadString
    call ParseInt
    mov num, eax

    ; Calculate the factorial
    mov ecx, num        ; Counter to count down to 1
    L1:
    cmp ecx, 1
    je done             ; Jump to done if ecx is 1
    imul fact, ecx      ; Multiply the current factorial by the current value of ecx
    dec ecx             ; Decrement ecx
    jmp L1

    done:
    ; Print the result
    mov edx, OFFSET factMsg
    call WriteString
    mov edx, num
    call WriteInt
    mov edx, OFFSET isMsg
    call WriteString
    mov edx, fact
    call WriteInt
    call Crlf
    exit

main ENDP
END main

@jere-mie
Copy link
Owner Author

TITLE Number Guessing Game

INCLUDE Irvine32.inc

.data
    prompt BYTE "Guess a number between 1 and 100: ", 0
    tooLowMsg BYTE "Too low! Try again.", 0
    tooHighMsg BYTE "Too high! Try again.", 0
    correctMsg BYTE "Congratulations, you guessed correctly!", 0
    buffer BYTE 11          ; Buffer to hold the user's input
    guess DWORD ?
    answer DWORD ?
    
.code
main PROC
    ; Generate a random number
    call Randomize
    mov eax, 100
    add eax, 1
    call RandomRange
    mov answer, eax

    ; Prompt the user to guess the number
    mov edx, OFFSET prompt
    call WriteString
    mov edx, OFFSET buffer
    mov ecx, LENGTHOF buffer
    call ReadString
    call ParseInt
    mov guess, eax

    ; Check if the guess is correct
    cmp guess, answer
    je correct
    jb tooLow
    jg tooHigh

    tooLow:
    mov edx, OFFSET tooLowMsg
    jmp printResult

    tooHigh:
    mov edx, OFFSET tooHighMsg
    jmp printResult

    correct:
    mov edx, OFFSET correctMsg

    printResult:
    call WriteString
    call Crlf
    exit

main ENDP
END main

@jere-mie
Copy link
Owner Author

TITLE Fibonacci Sequence

INCLUDE Irvine32.inc

.data
    prompt BYTE "Enter the number of terms to generate: ", 0
    termMsg BYTE "Term ", 0
    equalsMsg BYTE " = ", 0
    buffer BYTE 11              ; Buffer to hold the user's input
    numTerms DWORD ?
    fib1 DWORD 0
    fib2 DWORD 1
    fibNext DWORD ?
    count DWORD 2

.code
main PROC
    ; Get the number of terms from the user
    mov edx, OFFSET prompt
    call WriteString
    mov edx, OFFSET buffer
    mov ecx, LENGTHOF buffer
    call ReadString
    call ParseInt
    mov numTerms, eax

    ; Print the first two terms of the sequence
    mov edx, OFFSET termMsg
    call WriteString
    mov edx, 1
    call WriteInt
    mov edx, OFFSET equalsMsg
    call WriteString
    mov edx, fib1
    call WriteInt
    call Crlf
    mov edx, OFFSET termMsg
    call WriteString
    mov edx, 2
    call WriteInt
    mov edx, OFFSET equalsMsg
    call WriteString
    mov edx, fib2
    call WriteInt
    call Crlf

    ; Generate the remaining terms of the sequence
    L1:
    cmp count, numTerms
    jg done
    mov eax, fib2
    add eax, fib1
    mov fibNext, eax
    mov edx, OFFSET termMsg
    call WriteString
    mov edx, count
    call WriteInt
    mov edx, OFFSET equalsMsg
    call WriteString
    mov edx, fibNext
    call WriteInt
    call Crlf
    mov fib1, fib2
    mov fib2, fibNext
    inc count
    jmp L1

    done:
    call Crlf
    exit

main ENDP
END main

@jere-mie
Copy link
Owner Author

TITLE Prime Number Checker

INCLUDE Irvine32.inc

.data
    prompt BYTE "Enter an integer: ", 0
    primeMsg BYTE "The number is prime.", 0
    notPrimeMsg BYTE "The number is not prime.", 0
    buffer BYTE 11              ; Buffer to hold the user's input
    num DWORD ?
    divisor DWORD 2

.code
main PROC
    ; Get the integer from the user
    mov edx, OFFSET prompt
    call WriteString
    mov edx, OFFSET buffer
    mov ecx, LENGTHOF buffer
    call ReadString
    call ParseInt
    mov num, eax

    ; Check if the number is prime
    cmp num, 1
    jle notPrime
    cmp num, 2
    je prime
    mov eax, num
    mov edx, 0
    div divisor
    cmp edx, 0
    je notPrime
    mov eax, num
    mov ebx, 2
    mov ecx, eax
    dec ecx
    L1:
    mov edx, 0
    div ebx
    cmp edx, 0
    je notPrime
    inc ebx
    cmp ebx, ecx
    jle L1
    jmp prime

    prime:
    mov edx, OFFSET primeMsg
    call WriteString
    call Crlf
    jmp done

    notPrime:
    mov edx, OFFSET notPrimeMsg
    call WriteString
    call Crlf

    done:
    exit

main ENDP
END main

@jere-mie
Copy link
Owner Author

looks like a bunch of these don't work. I found 3 that do and will eventually fix the rest

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant