+1 (315) 557-6473 

Display 3 Random Numbers and A Fortune Cookie Phrase (At Random) Assignment Solution.


Instructions

Objective
Write a program in Flat Assembler (not Visual Studio) to display 3 random numbers and a fortune cookie phrase (at random), Assembly language.

Requirements and Specifications

Submit the asm source code file on Ecampus under the "Submit Homework" menu option.
Write an ×x86 assembly program that outputs three random numbers from 1-25 with no duplicates. Next, output a random inspirational quote.
Screenshots of output
program in Flat Assembler to display 3 random numbers and a fortune cookie phrase Assembly language
Source Code
format PE console
entry _start
include 'win32a.inc'
header: DB "Here are three distinct (not duplicate) random numbers from 1-25:", 10, 13, 0
msg1: DB "May the fourth be with you!", 10, 13, 0
msg2: DB "So long, and thanks for all the fish!", 10, 13, 0
msg3: DB "Where we're going, we don't need roads!", 10, 13, 0
msg4: DB "Live long and prosper!", 10, 13, 0
msg5: DB "It always seems impossible until it's done!", 10, 13, 0
intfmt: DB "%d", 10, 13, 0
newline: DB 10, 13, 0
num1: DD 0
num2: DD 0
num3: DD 0
section '.text' code readable executable
_start:
    push ebp
    mov ebp, esp
    ; initialize random generator
    push 0 ; push a zero
    call [time] ; get current time
    add esp, 4 ; restore stack
    push eax ; pass time to srand
    call [srand] ; set random seed
    add esp, 4 ; restore stack
    ; print header message
    push header ; load address of message
    call [printf] ; print the string
    add esp, 4 ; restore stack
    mov ebx, 25 ; load 25 for making divisions
    call [rand] ; generate a random number
    mov edx, 0 ; clear edx before division
    div ebx ; divide random number by 25 to get remainder 0-24
    inc edx ; add 1 to remainder to get number 1-25
    mov [num1], edx ; save number 1
    push edx ; pass number 1
    push intfmt ; pass integer format
    call [printf] ; print number 1
    add esp, 8 ; restore stack
gen2:
    call [rand] ; generate second random number
    mov edx, 0 ; clear edx before division
    div ebx ; divide random number by 25 to get remainder 0-24
    inc edx ; add 1 to remainder to get number 1-25
    cmp edx, [num1] ; see if number is repeated
    je gen2 ; if repeated, generate another one
    mov [num2], edx ; save number 2
    push edx ; pass number 2
    push intfmt ; pass integer format
    call [printf] ; print number 2
    add esp, 8 ; restore stack
gen3:
    call [rand] ; generate third random number
    mov edx, 0 ; clear edx before division
    div ebx ; divide random number by 25 to get remainder 0-24
    inc edx ; add 1 to remainder to get number 1-25
    cmp edx, [num1] ; see if number is repeated
    je gen3 ; if repeated, generate another one
    cmp edx, [num2] ; see if number is repeated
    je gen3 ; if repeated, generate another one
    mov [num3], edx ; save number 3
    push edx ; pass number 3
    push intfmt ; pass integer format
    call [printf] ; print number 3
    add esp, 8 ; restore stack
    ; generate random quote
    call [rand] ; generate second random number
    mov ebx, 5 ; load 5 to make division
    mov edx, 0 ; clear edx before division
    div ebx ; divide random number by 5 to get remainder 0-4
    cmp edx,0 ; if remainder is 0,
    je quote1 ; print quote 1
    cmp edx,1 ; if remainder is 1,
    je quote2 ; print quote 2
    cmp edx,2 ; if remainder is 2,
    je quote3 ; print quote 3
    cmp edx,3 ; if remainder is 3,
    je quote4 ; print quote 4
    jmp quote5 ; else, remainder is 4, print quote 5
quote1:
    mov eax, msg1 ; load quote 1 address
    jmp printQuote
quote2:
    mov eax, msg2 ; load quote 2 address
    jmp printQuote
quote3:
    mov eax, msg3 ; load quote 3 address
    jmp printQuote
quote4:
    mov eax, msg4 ; load quote 4 address
    jmp printQuote
quote5:
    mov eax, msg5 ; load quote 5 address
printQuote:
    ; print quote message
    push eax ; load address of message
    call [printf] ; print the string
    add esp, 4 ; restore stack
    call [getchar] ; wait until user presses a key
    mov ebp, esp
    pop ebp
    call [ExitProcess] ; exit the program
; Import section
section '.idata' import data readable
library kernel32, 'kernel32.dll', \
        msvcrt,'msvcrt.dll'
import kernel32, \
       ExitProcess,'ExitProcess'
import msvcrt, \
        printf, 'printf', time, 'time', \
        srand, 'srand', rand, 'rand', \
        getchar, 'getchar'