Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Assembly Language

Options
  • 25-11-2008 3:55pm
    #1
    Registered Users Posts: 2,328 ✭✭✭


    I'm currentyl doing an assignment where i have to print a 4x4 grid of random numbers 1-16.Each has to be included once i just need them in random order in the grid.

    Its the golden board problem if anyone recognises it. :)

    I've tried printing them out individually but i exceed the range i can jump with my repeat question at the end.

    Is ther any way i can create a grid and if so how do i access each individual block to place contents in?

    Any help much appreciated.
    Cheers


Comments

  • Registered Users Posts: 2,328 ✭✭✭gamblitis


    Can anyone help me out at all?


  • Registered Users Posts: 184 ✭✭stylers


    why the title "Assembly language" ??, and no reference to it afterwards :confused:. you'll really have to give more info about the problem than that for anyone to help..


  • Registered Users Posts: 7,468 ✭✭✭Evil Phil


    And post some code if you don't want the thread locked. You'll never learn anything if you get other people to do it for you.


  • Registered Users Posts: 2,328 ✭✭✭gamblitis


    .model small
    .data
    
    	CR equ 13
    	LF equ 10
    	
    	message1 db CR,LF, "1     $"
    	message2 db "2     $"
    	message3 db "3     $"
    	message4 db "4$"
    	message5 db CR,LF, "5     $"
    	message6 db "6     $"
    	message7 db "7     $"
    	message8 db "8$"
    	message9 db CR,LF, "9     $"
    	message10 db "10    $"
    	message11 db "11    $"
    	message12 db "12    $"
    	message13 db CR,LF, "13    $"
    	message14 db "14    $"
    	message15 db "15    $"
    	message16 db 5fh
    	blankline db CR,LF, "$"
    	digitpress db CR,LF, "To print again press 'r': $"
    	
    .code
    stack 100h
    	
    Program:
    
    	call initsegs
    start:
    	mov dx, OFFSET message1
    	call print
    	mov dx, OFFSET message2
    	call print
    	mov dx, OFFSET message3
    	call print
    	mov dx, OFFSET message4
    	call print
    	mov dx, OFFSET message5
    	call print
    	mov dx, OFFSET message6
    	call print
    	mov dx, OFFSET message7
    	call print
    	mov dx, OFFSET message8
    	call print
    	mov dx, OFFSET message9
    	call print
    	mov dx, OFFSET message10
    	call print
    	mov dx, OFFSET message11
    	call print
    	mov dx, OFFSET message12
    	call print
    	mov dx, OFFSET message13
    	call print
    	mov dx, OFFSET message14
    	call print
    	mov dx, OFFSET message15
    	call print
    	xor ax, ax
    	mov al, message16
    	mov dl, al
    	mov ah, 02h
    	int 21h
    	mov dx, OFFSET digitpress
    	call print
    	xor ax, ax
    	int 16h
    	mov bl, al
    	cmp bl, 'r'
    	je start
            call eXit
    
    ;--------------------Exit-----------------------------------
    PROC eXit
    		PUSH ax
    		mov ax, 4c00h
    		int 21h
    		POP ax
    	ret
    ENDP eXit
    ;--------------------EndExit-----------------------------------
    ;-------------Print------------------------------------
    PROC print
    		Push ax
    		mov ah, 9
    		int 21h
    		Pop ax
    	ret
    ENDP print
    ;----------EndPrint-------------------
    ;--------------------------Initsegs---------------------------------
    PROC initsegs
    		mov ax, @data
    		mov ds, ax
    	ret
    ENDP initsegs
    ;----------------------EndInitSegs-------------------------------------------
    
    
    END Program
    


    This what i have so far. This prints out the grid containing 1-16 in sequence.But what i need is for them to be printed out in a random order.`Pressing r repeats the print out but the randomisation thing is getting me.


  • Registered Users Posts: 68,317 ✭✭✭✭seamus


    You're looking for a random number generator.

    Assembly language doesn't come with pseudo-random generating functions, so I'm going to assume that's the primary reason why you've been given the task, so I'm not going to give you the answer. Google will give the same answer I was going to anyway.

    Rather than trying to pick a number and then insert into a random point in the grid, it would make more sense to pick a random number, then place that in the first spot in the grid. Then pick another random number, make sure it hasn't already been picked, and place it in the second spot on the grid, and so on...


  • Advertisement
  • Registered Users Posts: 1,228 ✭✭✭carveone


    seamus wrote: »
    You're looking for a random number generator.

    Yes indeedy. Google for it. In fact, given that I'm so old, I can remember that this might be a Usenet comp.lang.asm FAQ. Use the FAQ Luke... Or, given that you're probably in a University, Knuth's "Art of Computer Programming". This may involve going to a "library" (I know, I know, it's a tough life).

    Hum. Here's a silly solution off the top of my head (i'm suddenly moved to creativity). Take a number (call it the seed" and multiply it by another largish number. Store it back in the seed. Very hard. I mean, you have 10 numbers to shuffle, you aren't creating DES or something.

    .data
    rseed dw ?

    .code

    rand:
    mov ax,word ptr [rseed]
    mov dx, 9876h
    mul dx
    inc ax
    mov word ptr [rseed],ax
    ret

    (random returned in ax)

    I'll leave it up to you how to get ax to be a number from 1 to 10...
    Rather than trying to pick a number and then insert into a random point in the grid, it would make more sense to pick a random number, then place that in the first spot in the grid. Then pick another random number, make sure it hasn't already been picked, and place it in the second spot on the grid, and so on...

    Oooh, a bit brute force but hey, this will work for 10 numbers. Eventually. It wouldn't be good idea for 1000 numbers though. More algorithms (see library above) or take 10 numbers in an array and shuffle them...


  • Registered Users Posts: 9,579 ✭✭✭Webmonkey


    Amn't sure if will help or not, might be a bit too complex for just this case.

    Linear Freedback Shift Registers
    http://en.wikipedia.org/wiki/Linear_feedback_shift_register
    http://www.diycalculator.com/docs/demo-lfsr.pdf


  • Registered Users Posts: 2,082 ✭✭✭Tobias Greeshman


    seamus wrote: »
    You're looking for a random number generator.
    The most simplistic of random number generators tend to get a the current system time and multiply it by a large value. As the system time tends to always return something different this guarantees a certain degree on random.

    If you're doing this on Windows, the Dos "int 21h" set of functions should provide a mechanism for getting the system time. A quick search on google for the "int 21h" reference of functions should tell you how to do this.


Advertisement