Strona 1 z 1

Przesuwanie elementów tablicy.

: 23 lut 2018, 22:11
autor: JanJJW
Czy ktoś ma pomysł na procedurę przesuwania wartości w tablicy? Chodzi o to by tablica funkcjonowała jak stos. To znaczy by dopisanie kolejnego elementu spowodowało przesunięcie pozostałych wartości w górę (w dół) o jedną pozycję. Jakoś nic rozsądnego nie przychodzi mi do głowy :(

Re: Przesuwanie elementów tablicy.

: 24 lut 2018, 11:20
autor: Pikczu
Jeśli to ma obracać w kółko to proszę bardzo, przesuwanie przykład od samego Marka Albertsa.
Link do mcs forum: https://www.mcselec.com/index2.php?opti ... ift+array
  1. $regfile = "m8def.dat"
  2. $crystal = 12000000
  3. $hwstack = 32
  4. $framesize = 24
  5. $swstack = 16
  6.  
  7.  
  8. Declare Sub My_rotate_right(ar() As Byte , Byreg R24 As Byte)
  9.  
  10.  
  11. Dim Ar(20) As Byte , J As Byte
  12. Ar(1) = 1
  13.  
  14. Do
  15.    Call My_rotate_right(ar() , 5)
  16.  
  17.    For J = 5 To 1 Step -1
  18.       Print Bin(ar(j)) ; "  ";
  19.    Next
  20.    Print
  21. Loop
  22.  
  23. End
  24.  
  25.  
  26.  
  27.  
  28. Sub My_rotate_right(ar() As Byte , Byreg R24 As Byte)
  29.    Loadadr Ar(1) , X
  30.    !push r26
  31.    !push r27
  32.    !ld r16,x+  ; get byte
  33.    !lsl r16    ; lsl to left
  34.    !dec r24    ; just make sure passed byte > 1 !!!!!!!!
  35. Myrotate_lp1:
  36.    !ld r17,x   ; get  N byte
  37.    !rol r17    ; rotate left with carry
  38.    !st x+,r17  ; store and advance pointer
  39.    !dec r24     ; next byte
  40.    !brne myrotate_lp1
  41.    !pop r27
  42.    !pop r26
  43.    !adc r16,r24 ; add carry to bit 0
  44.    !st x,r16
  45. End Sub


******************************************************************edit*****************************************************
Fifo bufor zamień tylko stringi na bajty
Oryginał ma MCS https://www.mcselec.com/index2.php?opti ... ing+buffer

  1. Declare Sub Push_FIFO(char as string)
  2. Declare Function Pop_FIFO() as string
  3.  
  4. 'Set the size of the buffer
  5. Const FIFOSIZE = 10
  6.  
  7. 'Pad the buffer with one extra byte so it is easy to distinguish a full buffer
  8. 'from an empty one
  9. Const FIFOBUFSIZE = FIFOSIZE + 1
  10. Dim FIFO_Buffer(FIFOBUFSIZE) as Byte
  11.  
  12. 'Head and Tail point to the start and end of the buffer.  If head = tail, then
  13. 'the buffer is empty.
  14. Dim Head as Byte
  15. Dim Tail as Byte
  16.  
  17. '"Empty" the buffer
  18. Head = 1
  19. Tail = 1
  20.  
  21. 'Start of program
  22. Dim i as byte
  23. Dim temp as string * 1
  24. Dim MyData as string * 5
  25. MyData = "Hello"
  26.  
  27. 'Load the FIFO
  28. For I = 1 to 5
  29.    temp = mid(MyData, i, 1)
  30.    Call Push_FIFO( temp )
  31. Next
  32.  
  33. 'Now empty the data in the FIFO
  34. While Head <> Tail
  35.    Print POP_FIFO()
  36. Wend
  37.  
  38. End
  39.  
  40.  
  41. Sub Push_FIFO(char as String)
  42.    'Add a character to the FIFO
  43.    Local OldHead as byte
  44.  
  45.    'Save the current Head location to revert if the FIFO is full
  46.    OldHead = Head
  47.    Incr Head
  48.    'Handle wrapping around to the beginning
  49.    If Head > FIFOBUFSIZE then Head = 1
  50.  
  51.    If Head = Tail then
  52.       'FIFO is full.  Handle this however you want.  Here I will simply
  53.       'discard the new data and do nothing.
  54.       Head = Oldhead
  55.    Else
  56.       FIFO_Buffer(OldHead) = asc(char)
  57.    End If
  58. End Sub
  59.  
  60. Function Pop_FIFO() as String
  61.    'Return a character from the FIFO
  62.    Local Result as byte
  63.  
  64.    'Get the character at the tail of the buffer
  65.    Result = FIFO_Buffer(Tail)
  66.  
  67.    'Update the tail pointer
  68.    Incr Tail
  69.    If Tail > FIFOBUFSIZE then Tail = 1
  70.  
  71.    'Assign result as last instruction in the function
  72.    Pop_FIFO = Chr(Result)
  73. End Function

Re: Przesuwanie elementów tablicy.

: 24 lut 2018, 14:06
autor: JanJJW
Dziękuję bardzo. Przyda się :)

Re: Przesuwanie elementów tablicy.

: 25 lut 2018, 20:22
autor: JanJJW
Jeszcze raz dziękuję. Problem okazał się właściwie banalny. Dziękuję za naprowadzenie mnie na rozwiązanie.