Please use the really basic Assembly language, and also do the extra credit. mak
ID: 3676106 • Letter: P
Question
Please use the really basic Assembly language, and also do the extra credit. make sure your code will compile on visual studio 2015, and comment out the meaning of the code. Really appreciate
Filename: RandomString.asm
Create a procedure (UserInt) that asks for an unsigned integer input (N) from a user. The procedure must pass this integer back to the calling program as an argument (this means in a register).
Create a procedure (RandStr) that generates a string of length (L = 25), containing random capital letters. When calling the procedure, pass the value of L in EBX, and pass the offset of an array of bytes which will hold the string.
Write a program that uses both procedures, so that the input from the user determines how many times to generate a random string (not the same one over and over).
1. The output will be shown in the console window one string per line.
2. You may use any command Chapter 5 and below. Chapter 5 is about Procedures AKA subroutines or functions.
3. Your procedure must be called from the main procedure.
ExtraCredit: Print each string in a random color. This means that each individual string will be of the same random color.
Explanation / Answer
INCLUDE Irvine32.inc
.data
user_prompt BYTE "Enter an integer (from 1 to 20): ", 0 ; Display message asking for an Integer
arrayString byte 30 DUP(0),0 ; array for random strings
userinput DWORD ? ; for the user input interger so we can "pass back as an argument"
.code
main proc
call UserInt ; User input proc
call RandStr ; Random string proc
main endp
exit
UserInt proc
FAIL: ; jumps to here if they enter a number >21
mov edx, OFFSET user_prompt ; user prompt message
call WriteString ; writes the prompt to screen
call Crlf ; new line
call ReadInt ; reads the user input
cmp eax, 21 ; checks for valid input
ja FAIL ; Jumps to Fail for failing to enter the correct input ( Int from 1 to 20)
mov userinput, eax
;mov ecx, eax ; this is easier but i feel like i'll get points taken off for some reason...
; So the homework says "the proc must pass this int back to the calling program as an argument." Im not sure
; if you're wanting us to push this value to the stack or something fancy like that, for this program
ret ; I found it easier to just move it to ecx since its the counter for my loop anyways.. and I read that
UserInt ENDP ; arguments returned from procedures are stored in registers mainly. So I hope its ok! I could have pushed into a stack
; or made it a variable too if I wanted...
; ok reading it again i think i'll make it a variable but you could just mov ecx, eax
RandStr PROC Uses ESI ECX EAX
;-------------------------------------------------------------------------------------------
; Takes the user input from UserInt that was put into ECX
; and runs a loop that prints out random strings with
; capitol letters and random colors for the amount of the user input
;Receives: ESI = the array
; ECX = User input from UserInt proc
; EAX = random characters
;Returns: userinput number of random capitol letter strings in random colors
;--------------------------------------------------------------------------------------------
mov ecx, userinput ; the argument that we got passed
L1: ; generates the string
push ecx ; save our counter for userinput # of strings
mov ecx, 30 ; number of letters in each string
mov esi,OFFSET arrayString ;string index
L2: ; loop for our random ints
mov eax,26 ; random int from 0 - 25
call RandomRange ; Library function random number from previous values stores to EAX
add eax,'A' ; - totally suprised this worked...but i'll take it!
mov [esi],al ; insert random char into array
inc esi ; incrament the pointer
loop L2
mov edx,OFFSET arrayString ; puts it in EDX for output to screen
call settextc ; sets random color
call WriteString ; writes to screen
call Crlf
pop ecx ; gets back our loop counter from userinput
loop L1
ret
RandStr ENDP
settextc proc ; Shameless stole this proc from group 4! but honestly it's not that hard to figure out
push eax; ; actually they made 1 mistake
mov eax, 15; ; since settextcolor is 0 to n-1 and the colors go from 0 to 15 they had 14 which means you would never get white
call RandomRange;
add eax, 1; ; so we dont get black
call SetTextColor;
pop eax;
ret;
settextc endp
END main