[scienze.unimo] Re: Architettura - only 4 Info

  • From: Valerio Bigiani <vbigiani@xxxxxxxx>
  • To: scienze.unimo@xxxxxxxxxxxxx
  • Date: Thu, 22 Jun 2006 00:53:02 +0200

Testato col mio assemblatore ( http://linuz.sns.it/~v.bigiani/files/ArcCalc.zip 
):

# algoritmo banale di elevamento a potenza per calcolare a ^ b:
#
# leggi A e B
# RIS <- 1
# alloco ciclo_molt e C; ZERO e' richiesto per fare != 0
# while (B != 0)
#   ciclo_molt = A
#   C <- 0
#   while (ciclo_molt != 0)
#     C <- C + RIS
#     ciclo_molt --
#   RIS <- C
#   B --
# restituisci RIS
# il tempo di calcolo e' O(A * B).

JMP PROG
A:
B:
C: 0
CICLO_MOLT :
RIS: 1
ZERO : 0

PROG :
  # leggi A e B da tastiera; voglio calcolare a ^ b
  RD A
  RD B
  # if B = 0 then fine
  LD B
  SUB ZERO
  JZ FINE
  #
  CICLO_EXP :
    # ciclo_molt = A
    LD A
    ST CICLO_MOLT
    # C <- 0
    LD ZERO
    ST C
    CICLO_MOL_ADDRESS :
      # C <- C + RIS
      LD C
      ADD RIS
      ST C
      # ciclo_molt --
      LD CICLO_MOLT
      DEC
      ST CICLO_MOLT
      # chiudo il while
      JNZ CICLO_MOL_ADDRESS
    # RIS <- C
    LD C
    ST RIS
    # B --
    LD B
    DEC
    ST B
    # chiudo il ciclo
    JNZ CICLO_EXP
FINE :
PRN_D RIS
HLT

Other related posts: