First post! Help, i'd like to know the time!

Started by Seed_87, July 19, 2008, 10:42:44 PM

Previous topic - Next topic

Seed_87

Hello there! This is my first post in this forum..  :U

So, I'm a total newbie when it comes to programming in assembler. I only know C++ and Java and have absolutely no idea on how to even begin to use assembler so I figured i'd read a manual or two and, now that I think I  may have an idea of the basics, Im trying to start by making very simple apps and procedures and stuff so..

What do I need to do if I wanted to make a procedure that would return a random number between 0....n ?
I thought maybe there's a function or something like GetSystemTime in C??

Remember im a total noob at this :(

Many thanks, any help is very appreciated!
Alex.

raymond

Here's the kind of procedure I use for generating pseudo-random numbers which are sufficiently random, for example, to shuffle a deck of cards.

I use GetTickCount which will returns in EAX the number of milliseconds since Windows was started up. That's sufficently random to use as a "seed". Make sure it's odd before storing it. Then use a relatively large prime (such as 3985951) to multiply the seed and retain only the lower 32 bits of the product which you use for generating your random number.

Example:

.data
seed   dd  ?
n      dd  52     ;example for a deck of cards
prime  dd  3985951

.code
call GetTickCount
or   al,1         ;insures EAX is odd
mul  prime        ;randomize it a bit more
mul  prime
mov  seed,eax

;then, when you need a random number X, 0<= X <n
call RandomNumber

RandomNumber:
mov  eax,seed
mul  prime
mov  seed,eax      ;keep the lower 32 bits for the next seed
mul  n             ;your random number is now in EDX
ret


Hope this helps
When you assume something, you risk being wrong half the time
http://www.ray.masmcode.com

Seed_87

Hey thanx raymond! 

I'll try my best to try and understand the code you gave me :eek

Thanx a lot!  :U

Ill be probably back with some questions  :green


Alex

Seed_87

Now thats cool, i think im getting the hang of it..  :green2

Now i got a next question for you: The code for outputting to the cosole is the one below, only when I compile it, it throws this error:

ERROR A2022: Instruction operands must be of the same size  :'(

Does that have anything to do with the fact that db is 1 byte long and dx is 2 bytes long? ( Am i wrong with this?  :eek )

Oh, i hope im not asking too much but: Outputting a number? Is it the same code?




    .486                                        ; Para crear codigo de 32 bits
    .model flat, stdcall                      ; modelo de memoria de 32 bits
    option casemap :none                ; case sensitive

    include \masm32\include\windows.inc     
    include \masm32\macros\macros.asm       
    include \masm32\include\kernel32.inc


.data                       
                     
    msg   db "this is the string to print",13,10,"$"


  .code                       ; Aqui comienza el codigo como tal...

; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««

start:                              ; The CODE entry point to the program


     mov  dx, offset msg      ; dx register gets location of string to be printed

     mov  ah,09h        ; function code goes into the ah register

     int  21h           ; tell DOS to print the string


   exit

end start                       ; Tell MASM where the program ends


Neil

Your problem is that you are telling the assembler it is 32 bit code & you are trying to write 16 bit. I think this should be moved to the DOS forum

Seed_87

Thanx Neil,  will post over there! btw raymond your code ROCKS!  :dance:

Alex

jj2007

Just out of curiosity: Do you WANT to program in DOS? Win32 is a lot easier, and a lot more powerful...
Here is a little demo - a macro that may help you debug your programs. Usage:

   mbDebug 0, "Show me the registers and five variables:", \
   eax, ecx, edx, esi, edi, ebx, ebp, esp, \
   MyCounter, MyThirdVar, MyFilePath$, MyGreets$

Backslash is line continuation character.

include \masm32\include\masm32rt.inc

EolMode equ 13, 10
; EolMode equ 32,32,32,32 ; for horizontal display

mbDebug MACRO slot:REQ, txt_title:REQ, args:VARARG
pushad
pushfd
ifndef mbDebugbuff
.data?
mbDebugbuff db 2048 dup (?)
.data
mbDebugbptr dd mbDebugbuff
mbDebugStop dd 5 dup (IDOK)
.code
endif
push offset mbDebugbuff
pop mbDebugbptr
mov byte ptr mbDebugbuff, 0
for arg, <args>
LOCAL tmp$, cc
cc INSTR <arg>, <$>
pushad
tmp$ CATSTR <chr$(">, <arg>, <")>
if cc
mov mbDebugbptr, cat$(mbDebugbptr, tmp$, chr$(9), <arg>, chr$(EolMode))
else
mov mbDebugbptr, cat$(mbDebugbptr, tmp$, chr$(9), sstr$(arg), chr$(EolMode))
endif
popad
ENDM
.if mbDebugStop+4*slot==IDOK
invoke MessageBox, 0, addr mbDebugbuff, reparg(txt_title), MB_OKCANCEL
.endif
mov mbDebugStop+4*slot, eax
popfd
popad
ENDM

.code

txMyPath db "MulDbgMacro.asm", 0
txMyGreets db 13,10,10, "Thanks to JimG for the ", 34, "fine tuning", 34, " ;-)", 0
MyFilePath$ dd txMyPath
MyGreets$ dd txMyGreets

start:

    call main
    exit

main proc
    LOCAL MyCounter:DWORD, My2ndVar:DWORD, MyThirdVar:DWORD
    mov MyCounter, 111
    mov My2ndVar, 222
    mov MyThirdVar, 333
    .While MyCounter>0
pushad
mov eax, 1000001
mov ecx, 1000002
mov edx, 1000003
mov esi, 1000004
mov edi, 1000005
mov ebx, 1000006
mbDebug 0, "Show me the registers and five variables:", \
eax, ecx, edx, esi, edi, ebx, ebp, esp, \
MyCounter, MyThirdVar, MyFilePath$, MyGreets$
dec MyCounter
mbDebug 1, "Show me the vars:", MyCounter, MyThirdVar, MyFilePath$
popad
    .Endw
    ret

main endp
end start

Seed_87

wow that looks complicated.. only it isnt that complicated  :U

Thanx a lot for that jj2007. I'm really thankful I registered in this forum cause i was really lost  :eek

Ill take into consideration everything you said and try and program only in 32bit code.

Thanx again!


Alex.

jj2007

You are welcome :bg
As a start, check the \masm32\examples folder, or google for Iczelion tutorials.