dimanche 24 juin 2018

Why is the first and second image of the spin slot cant never be the same?

i have a project in assembly that i have written, its a spin slot with 3 picture option. i dont know why' ive been sitting hours and couldn't find why the first and second image slot can't never be the same and that's causes the win condition can never be achived.

this is the code:

IDEAL
MODEL small
STACK 100h
DATASEG
; --------------------------
; your variables here
    ; name of image file
    va db 1,2,3,4   
    filename db 'pic2.bmp',0      
    smallpic1 db 'sppic1.bmp',0   
    smallpic2 db 'sppic2.bmp',0     
    smallpic3 db 'sppic3.bmp',0     
    smallpic4 db 'sppic4.bmp',0     
    smallpic5 db 'sppic5.bmp',0    
    ; x and y ccordinates of imag left down corner
    x_coord dw 0
    y_coord dw 200
    image_height dw 140   
    ; must be an 8 multiplication
    image_width dw 320 

    ; image file handle - received 
    ; when opening a file
    filehandle dw ?                 

    ; image file header
    Header db 54 dup (0)

    ; color table
    Palette db 256*4 dup (0)

    ; a buffer holding one image line 
    ; must be an 8 multiplication
    ScrLine db 320 dup (0)

    ; printed when we fail to open file
    ErrorMsg db 'Error', 13, 10,'$'

    diff dw ?  
forstars db ' for start press s',10,13,'$'    
instruction db ' to roll the spin slot press on enter',10,13,' and see what you get, if the three',10,13,' picture are the same you win, for',10,13,' rolling it again press on enter',10,13,' and for exit press e$'

MAX_TICKS db 20 
lastTick db 0   
TickCounter db 0  
adda db '   to roll the spin slot press enter$'
; --------------------------
CODESEG
proc OpenFile
; Open image file file
    mov ah, 3Dh
    xor al, al

    int 21h
    jc openerror
    mov [filehandle], ax
    ret
openerror:
    mov dx, offset ErrorMsg
    mov ah, 9h
    int 21h
    ret
endp OpenFile

; close file at end of use
proc CloseFile
    push bx
    push ax
    mov bx, [filehandle]
    mov ah, 3Eh
    int 21h
    pop ax
    pop bx
    ret
endp CloseFile
proc ReadHeader
; Read BMP file header, 54 bytes
    mov ah,3fh
    mov bx, [filehandle]
    mov cx,54
    mov dx,offset Header
    int 21h
    ret
endp ReadHeader

proc ReadPalette
; Read BMP file color palette, 256 colors * 4 bytes (400h)
    mov ah,3fh
    mov cx,400h
    mov dx,offset Palette
    int 21h
    ret
endp ReadPalette

proc CopyPal
; Copy the colors palette to the video memory
; The number of the first color should be sent to port 3C8h
; The palette is sent to port 3C9h
    mov si,offset Palette
    mov cx,256
    mov dx,3C8h
    mov al,0
    ; Copy starting color to port 3C8h
    out dx,al
    ; Copy palette itself to port 3C9h
    inc dx
PalLoop:
    ; Note: Colors in a BMP file are saved as BGR values 
    ; rather than RGB.
    mov al,[si+2] ; Get red value.
    shr al,2 ; Max. is 255, but video palette maximal
    ; value is 63. Therefore dividing by 4.
    out dx,al ; Send it.
    mov al,[si+1] ; Get green value.
    shr al,2
    out dx,al ; Send it.
    mov al,[si] ; Get blue value.
    shr al,2
    out dx,al ; Send it.
    add si,4 ; Point to next color.
    ; (There is a null chr. after every color.)

    loop PalLoop
    ret
endp CopyPal


proc CopyBitmap
; BMP graphics are saved upside-down.
; Read the graphic line by line (200 lines in VGA format),
; displaying the lines from bottom to top.
    mov ax, 0A000h
    mov es, ax

    ; calculate gap between screen buttom and down corner
    mov ax, [y_coord]
    sub ax, [image_height]
    mov [diff], ax

    ;number of lines - loop iterations 
    mov cx,[image_height]  
PrintBMPLoop:
    push cx ; keep cx - because it is used in loop

    ; di = cx*320, point to the correct screen line
    add cx, [diff]
    mov di,cx
    shl cx,6
    shl di,8
    add di,cx
    add di, [x_coord]

    ; Read one line
    mov ah,3fh
    mov cx,[image_width]
    mov dx,offset ScrLine
    int 21h

    ; Copy one line into video memory
    cld ; Clear direction flag, for movsb
    mov cx,[image_width]
    mov si,offset ScrLine

    rep movsb ; Copy line to the screen
     ;rep movsb is same as the following code:
     ;mov es:di, ds:si
     ;inc si
     ;inc di
     ;dec cx
     ;loop until cx=0
    pop cx
    loop PrintBMPLoop
    ret
endp CopyBitmap

proc startphoto
; Process BMP  file
    call OpenFile
    call ReadHeader
    call ReadPalette
    call CopyPal
    call CopyBitmap
    call CloseFile
    ret
endp startphoto

proc clean
;taxt mood  
    push ax
    mov ax, 2 
    int 10h
; Graphic mod
    mov ax, 13h
    int 10h
    pop ax
    ret
endp clean

proc randomnumber
random1:
; put segment number in 
; register es
    mov ax, 40h
    mov es, ax

; move random number to ax
    mov ax, [es:6Ch]
    xor ax, [bx]
    inc bx
    and ax, 15
    cmp ax, 4
    ja random1
    ret 
endp randomnumber

proc sfphoto
;small first pic
    mov [x_coord], 50
    mov [y_coord], 100
    mov [image_height],40
    mov [image_width],  40
    mov dx, offset smallpic1
    call startphoto
    ret 
endp sfphoto

proc sfphoto1
;small first pic
    mov [x_coord], 140
    mov [y_coord], 100
    mov [image_height],40
    mov [image_width],  40
    mov dx, offset smallpic1
    call startphoto
    ret 
endp sfphoto1

proc sfphoto2
;small first pic
    mov [x_coord], 230
    mov [y_coord], 100
    mov [image_height],40
    mov [image_width],  40
    mov dx, offset smallpic1
    call startphoto
    ret 
endp sfphoto2


proc ssphoto
;small second pic
    mov [x_coord], 50
    mov [y_coord], 100
    mov [image_height],40
    mov [image_width],  40
    mov dx, offset smallpic2
    call startphoto
    ret 
endp ssphoto

proc ssphoto1
;small second pic
    mov [x_coord], 140
    mov [y_coord], 100
    mov [image_height],40
    mov [image_width],  40
    mov dx, offset smallpic2
    call startphoto
    ret 
endp ssphoto1

proc ssphoto2
;small second pic
    mov [x_coord], 230
    mov [y_coord], 100
    mov [image_height],40
    mov [image_width],  40
    mov dx, offset smallpic2
    call startphoto
    ret 
endp ssphoto2


proc sthphoto
;small therd pic
    mov [x_coord], 50
    mov [y_coord], 100
    mov [image_height],40
    mov [image_width],  40
    mov dx, offset smallpic3
    call startphoto
    ret 
endp sthphoto

proc sthphoto1
;small therd pic
    mov [x_coord], 140
    mov [y_coord], 100
    mov [image_height],40
    mov [image_width],  40
    mov dx, offset smallpic3
    call startphoto
    ret 
endp sthphoto1

proc sthphoto2
;small therd pic
    mov [x_coord], 230
    mov [y_coord], 100
    mov [image_height],40
    mov [image_width],  40
    mov dx, offset smallpic3
    call startphoto
    ret 
endp sthphoto2

proc sfophoto
;small four pic
    mov [x_coord], 50
    mov [y_coord], 100
    mov [image_height],40
    mov [image_width],  40
    mov dx, offset smallpic4
    call startphoto
    ret 
endp sfophoto

proc sfophoto1
;small four pic
    mov [x_coord], 140
    mov [y_coord], 100
    mov [image_height],40
    mov [image_width],  40
    mov dx, offset smallpic4
    call startphoto
    ret 
endp sfophoto1

proc sfophoto2
;small four pic
    mov [x_coord], 230
    mov [y_coord], 100
    mov [image_height],40
    mov [image_width],  40
    mov dx, offset smallpic4
    call startphoto
    ret 
endp sfophoto2

proc sfiphoto
;small five pic
    mov [x_coord], 50
    mov [y_coord], 100
    mov [image_height],40
    mov [image_width],  40
    mov dx, offset smallpic5
    call startphoto
    ret 
endp sfiphoto

proc sfiphoto1
;small five pic
    mov [x_coord], 140
    mov [y_coord], 100
    mov [image_height],40
    mov [image_width],  40
    mov dx, offset smallpic5
    call startphoto
    ret 
endp sfiphoto1

proc sfiphoto2
;small five pic
    mov [x_coord], 230
    mov [y_coord], 100
    mov [image_height],40
    mov [image_width],  40
    mov dx, offset smallpic5
    call startphoto
    ret 
endp sfiphoto2


; read_clock - A pocedure for clock reading
; when done clock values ar in cx and dx
proc read_clock
    push    ax
    mov     ah, 2ch
    int     21h     ; clock interrupt
    pop     ax
    ret
endp read_clock

;FIRST CLOCK
; wait_ticks - the below procedure waits for 
; MAX_TICKS clock ticks  -  this is done by reading the clock
; and counting tick changes in dl
proc wait_ticks
    ; keep registers
    push    dx
    push    cx
    push    ax

    mov al, [MAX_TICKS]
    ; read clock to initiate lastTick
    call    read_clock
    mov     [lastTick], dl
    mov     [tickCounter], 0

ticksLoop:
    ;read the clock and check if it was changed
    call    read_clock
    cmp     [lastTick], dl
    je      ticksLoop       ; no tick - do nothing - jump back to ticksLoop

    ; clock was changed - a new tick
    inc     [tickCounter]
    cmp     [tickCounter], al
    je      timeover

    ; MAX_TICK was not reached
    mov     [lastTick], dl
    jmp     ticksLoop
timeover:
    pop     ax
    pop     cx
    pop     dx
    ret
endp wait_ticks



proc game 
    call randomnumber
;compering
    cmp ax, 00
    je one 
    cmp ax, 01
    je two
    cmp ax, 02
    je three
    cmp ax, 03 
    je four
    cmp ax, 04
    je five
one:
;small first pic
    call sfphoto
    jmp ffph
two:
;small second pic
    call ssphoto
    jmp ffph
three:
;small therd pic
    call sthphoto
    jmp ffph
four:
;small four pic
    call sfophoto
    jmp ffph
five:
;small five pic
    call sfiphoto
    jmp ffph
ffph:
;start printing second picture
    call wait_ticks
    call randomnumber
;compering
    cmp ax, 00
    je one1
    cmp ax, 01
    je two1
    cmp ax, 02
    je three1
    cmp ax, 03 
    je four1
    cmp ax, 04
    je five1
one1:
;small first pic
    call sfphoto1
    jmp fsph
two1:
;small second pic
    call ssphoto1
    jmp fsph
three1:
;small therd pic
    call sthphoto1
    jmp fsph
four1:
;small four pic
    call sfophoto1
    jmp fsph
five1:
;small five pic
    call sfiphoto1
    jmp fsph
fsph:
;start printing therd picture
    mov [MAX_TICKS], 20
    call wait_tickS
    call randomnumber
    ;compering
    cmp ax, 00
    je one2
    cmp ax, 01
    je two2
    cmp ax, 02
    je three2
    cmp ax, 03 
    je four2
    cmp ax, 04
    je five2
one2:
;small first pic
    call sfphoto2
    jmp fthph
two2:
;small second pic
    call ssphoto2
    jmp fthph
three2:
;small therd pic
    call sthphoto2
    jmp fthph
four2:
;small four pic
    call sfophoto2
    jmp fthph
five2:
;small five pic
    call sfiphoto2
    jmp fthph
fthph:
    ret
endp game


proc afterClean
    mov [MAX_TICKS], 20
    call wait_ticks
    ret 
endp afterClean


proc music
    mov     al, 182         ; Prepare the speaker for the
        out     43h, al         ;  note.
        mov     ax, 4560       ; Frequency number (in decimal)
                                ;  for middle C.
        out     42h, al         ; Output low byte.
        mov     al, ah          ; Output high byte.
        out     42h, al 
        in      al, 61h         ; Turn on note (get value from
                                ;  port 61h).
        or      al, 00000011b   ; Set bits 1 and 0.
        out     61h, al         ; Send new value.
        mov     bx, 25          ; Pause for duration of note.
pause1:
        mov     cx, 65535
pause2:
        dec     cx
        jne     pause2
        dec     bx
        jne     pause1


        in      al, 61h         ; Turn off note (get value from
                                ;  port 61h).
        and     al, 11111100b   ; Reset bits 1 and 0.
        out     61h, al         ; Send new value.

        ret 
endp music


start:
    mov ax, @data
    mov ds, ax
; --------------------------
    mov bx, 0
; Your code here
s:
; Graphic mode
    mov ax, 13h
    int 10h
;printing string
    mov  dx, offset forstars 
    mov  ah, 9h 
    int  21h
;printing string
    mov  dx, offset instruction 
    mov  ah, 9h 
    int  21h
; first pic 
    mov dx, offset filename
    call startphoto
; קריאת תו ופעולה בהתאם
    mov ah, 0h
    int 16h 
    cmp al, 's'
    jne a1
    call music
    call clean
    je startrall
a1: 
    cmp al, 'e'
    jne s
    call music
    je exit1
startrall:  
;printing string
    mov  dx, offset adda 
    mov  ah, 9h 
    int  21h
;קריאת תו ופעולה בהתאם 
    mov ah, 0h
    int 16h 
    cmp al, 13
    jne f1
    call music
    je startgame
f1: 
    cmp al, 'e'
    jne startrall
    call music
    je exit1
startgame:
    call clean
    call afterClean
    call game
;קריאת תו ופעולה בהתאם
tav1:
    mov ah, 0h
    int 16h 
    cmp al, 13
    jne tav2 
    call music
    call clean 
    call afterClean
    je startgame    
;קריאת תו ופעולה בהתאם
tav2:
    cmp al, 'e'
    jne tav1
    call music
    je exit1
exit1:
    call clean

; --------------------------

exit:
    mov ax, 4c00h
    int 21h
END start

the first and second slot will always be the same picture, i cant find why the random can never be the same.




Aucun commentaire:

Enregistrer un commentaire