Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Guessuing_Game/LimitedRandomValues(0-5).asm
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
52 lines (50 sloc)
1.92 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
; assembly code that takes user input and creates | |
; a set of random numbers if called repeatedly using for example | |
; a loop. | |
section .data | |
seedMsg db "Put in 8 characters to act as the basis for random value: " ;Ask the user to put in 8 characters, for example their name | |
seedMsgLen equ $-seedMsg ; Get the length of the message | |
section .bss | |
num1 resb 8 ; variable to store random number | |
seed resb 8 ; variable to store random number seed | |
section .text | |
global _start | |
_start | |
mov rax,4 ;; Move the system call 4 to the register rax | |
; to say name question | |
mov rbx,1 ;; put the value 1 in the register rbx - | |
; standard output to the screen | |
mov rcx, seedMsg ; Put the offset of name in rcx register | |
;; so we can access the string 'name' | |
mov rdx, seedMsgLen ;Move length of name in register | |
int 80h ; Call the operating system with an interrupt. | |
mov rax,3 ; system call input | |
mov rbx, 0 ; stand input | |
mov rcx, seed ; store inputted value in answer - note ascii | |
; value and should be a name of up to 6 characters | |
mov rdx, 8 ; size of input value in bytes | |
int 80h ; interrupt | |
mov rax, [seed] ; put the name into the register | |
call random ; call random | |
call random ; call random | |
call end ; call end | |
; random function | |
random: | |
xor rdx,rdx ; ensure that rdx is empty to stop overflow | |
mov rbx, 6 ;To get 0 to 5 move 6 into | |
div rbx ;Div rax by rbx put remainder in rdx | |
add rdx, '0' ; make remainder ascii to print | |
push rax ; put rax on the stack | |
mov [num1], rdx ; put ascii remainder in num1 | |
mov eax,4 ; system call print to screen | |
mov ebx, 1 ; standard out | |
mov ecx, num1 ; ascii remainder | |
mov edx, 1 ; size of output | |
int 80h ; interrupt | |
pop rax ; pop off stack | |
shr rax, 1 ; shift the binary value right by one 0101101 becomes 0010110. To make more random can use shl (shift left) vary the number of digits moved | |
ret ; return | |
; end function | |
end: | |
mov eax, 1 ; exit system call | |
int 80h ; interrupt |