Reputation: 67
I have recently started to learn assembly language. For a project I am working on I have to make a random number generator using linear congruence. I am suppose to take in three numbers. An upper bound, a lower bound and a number of how many random numbers I want. As for the formula for getting a random number I came up with....
randomNumber = (seed % (upper-lower) + lower)
I then tried to put this into code. I came up with this
.data
upper BYTE 100 ;setting upper limit 100
lower BYTE 0 ;setting lower limit 0
number BYTE 5 ;number of random numbers
.code
call main
exit
main proc
cls
mov bx,upper ;moving upper bound into bx
mov dx,lower ;moving lower bound into dx
mov ax,2914017 ;taking a random number for this trial
mov ecx,number ;setting the loop counter
L1:
sub bx,dx ;(upper-lower)
div bx
add ah,dx ;(randomNumber mod (bx) + lower
main endp
I am curious how I would print out the random number at the end of each loop cycle. And if the above code makes any sense.
Thanks in advance!
Upvotes: 3
Views: 14955
Reputation: 1442
The answer to your question is platform dependent, so you need to specifiy this, and we can help you in a more specific way.
But a common method in real-address-mode (e.g. MS-DOS) is to call an interrupt and send the data to the SO (Standard Output).
Example (the %macro keywords are preprocessor directives that NASM understands - but not all compilers use it, but I included it for clarity):
%macro printLn 1
mov ah, 09h ; 9h means "write a string" on screen
mov edx, %1
int 21h ; call the ISR for MS-DOS Services
%endmacro
[SECTION .text]
mystart:
printLn string
ret
[SECTION .data]
string db "This is a string of text", 13, 10, '$'
Another method might be to hook directly a C-Lib function and output that way.
You should refer to the ASCII Table for character codes.
Also remember, as drhirsch mentioned below, that if you intend to output integers, you will need to convert those values into a form that your ISR can handle, like hex equivalents. You will then need to write an additional function to "translate" the values. This question is a good example of what I am referring to.
Upvotes: 1