Advertisement
Guest User

Untitled

a guest
Jun 3rd, 2011
2,257
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ;Recursive Ackermann-Peter-Function in x86 nasm-assembly
  2. ;@AUTHOR: Alexander Schneider, Düsseldorf, Germany
  3. ;Ackermann-Peter-Function:
  4. ;A(n,m) = m + 1 if n == 0
  5. ;A(n,m) = A(n-1, 1) if n > 0 && m == 0
  6. ;A(n,m) = A(n-1, A(n, m-1)) if n > 0 && m > 0
  7. ;
  8. ;ecx is return the register
  9.  
  10. %include "asm_io.inc"
  11.  
  12. segment .data
  13.  
  14.     prompt1 db "Bitte n (>=0) für die Ackermann-Berechnung eingeben: ",10,0
  15.     prompt2 db "Bitte m (>=0) für die Ackermann-Berechnung eingeben: ",10,0
  16.     prompt3 db "Das Ergebniss ist: ",0
  17.     promptError db "n und m müssen beide >= 0 sein",10,0
  18.     debug db "Debug-Message",10,0
  19.    
  20. segment .bss
  21.  
  22. segment .data
  23.     global asm_main
  24. asm_main:
  25.     ENTER 0,0               ;setup Routine
  26.     PUSHA
  27.     MOV eax, prompt1        ;ask for n
  28.     CALL print_string
  29.     CALL read_int
  30.     CMP eax, 0              ;check n
  31.     JL inputError
  32.     MOV ebx, eax            ;save n
  33.     MOV eax, prompt2        ;ask for m
  34.     CALL print_string
  35.     CALL read_int
  36.     CMP eax, 0              ;check m
  37.     JL inputError
  38.     MOV edx, eax            ;save m
  39.     MOV ecx, 0
  40.     PUSH edx                ;push m
  41.     PUSH ebx                ;push n
  42.     CALL ackermann
  43.     POP ebx
  44.     POP edx
  45.     JMP result
  46.  
  47. ackermann:
  48.     ENTER 0,0
  49.     MOV edx, [ebp+12]       ;get m
  50.     MOV ebx, [ebp+8]        ;get n
  51.     CMP ebx, 0
  52.     JE firstcase
  53.     CMP edx, 0
  54.     JE secondcase
  55.     ;inner ackermann return for m
  56.     DEC edx
  57.     PUSH edx
  58.     PUSH ebx
  59.     CALL ackermann
  60.     POP ebx
  61.     POP edx
  62.     DEC ebx
  63.     PUSH ecx                ;return from last call is m
  64.     PUSH ebx
  65.     CALL ackermann
  66.     POP ebx
  67.     POP edx
  68.     LEAVE
  69.     RET
  70.  
  71.    
  72. firstcase:
  73.     MOV ecx, edx
  74.     INC ecx
  75.     LEAVE
  76.     RET
  77.    
  78. secondcase:
  79.     DEC ebx
  80.     PUSH dword 1            ;push m
  81.     PUSH ebx                ;push n
  82.     CALL ackermann
  83.     POP ebx
  84.     POP edx
  85.     LEAVE
  86.     RET
  87.    
  88. result:
  89.     MOV eax, prompt3
  90.     CALL print_string       ;print result
  91.     MOV eax, ecx
  92.     CALL print_int
  93.     CALL print_nl
  94.    
  95. end:
  96.     POPA
  97.     MOV eax, 0              ;return Value for C-driver
  98.     LEAVE
  99.     RET
  100.  
  101. inputError:                 ;n or m was <0
  102.     MOV eax, promptError
  103.     CALL print_string
  104.     JMP end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement