Advertisement
Guest User

Untitled

a guest
Jun 2nd, 2011
336
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. %include "asm_io.inc"
  9.  
  10. segment .data
  11.  
  12.     prompt1 db "Bitte n (>=0) für die Ackermann-Berechnung eingeben: ",10,0
  13.     prompt2 db "Bitte m (>=0) für die Ackermann-Berechnung eingeben: ",10,0
  14.     prompt3 db "Das Ergebniss ist: ",0
  15.     promptError db "n und m müssen beide >= 0 sein",10,0
  16.    
  17. segment .bss
  18.  
  19. segment .data
  20.     global asm_main
  21. asm_main:
  22.     ENTER 0,0               ;setup Routine
  23.     PUSHA
  24.     MOV eax, prompt1        ;ask for n
  25.     CALL print_string
  26.     CALL read_int
  27.     CMP eax, 0              ;check n
  28.     JL inputError
  29.     MOV ebx, eax            ;save n
  30.     MOV eax, prompt2        ;ask for m
  31.     CALL print_string
  32.     CALL read_int
  33.     CMP eax, 0              ;check m
  34.     JL inputError
  35.     MOV edx, eax            ;save m
  36.     PUSH ebp
  37.     PUSH edx                ;push m
  38.     PUSH ebx                ;push n
  39.     CALL ackermann
  40.     POP ebx
  41.     POP edx
  42.     POP ebp
  43.     JMP end
  44.  
  45. ackermann:
  46.     MOV edx, [ebp+8]        ;get m from stack
  47.     MOV ebx, [ebp+12]       ;get n from stack
  48.     CMP ebx, 0              ;check if n is 0
  49.     JE firstcase
  50.     CMP edx, 0              ;check if m is 0
  51.     JE secondcase
  52.     DEC ebx
  53.     PUSH ebp
  54.     PUSH edx
  55.     PUSH ebx
  56.     CALL ackermann
  57.     PUSH ebx
  58.     PUSH eax
  59.     CALL ackermann
  60.     POP ebx
  61.     POP edx
  62.     POP ebp
  63. end:
  64.     MOV ebx, eax            ;save result
  65.     MOV eax, prompt3
  66.     CALL print_string       ;print result
  67.     MOV eax, ebx
  68.     CALL print_int
  69.     CALL print_nl
  70.     POPA
  71.     MOV eax, 0              ;return Value for C-driver
  72.     LEAVE
  73.     RET
  74.  
  75. firstcase:
  76.     MOV eax, [ebp+8]        ;Put m+1 in return Register
  77.     INC eax                 ;
  78.     RET                     ;return
  79.    
  80. secondcase:
  81.     MOV ebx, [ebp+12]       ;get n
  82.     DEC ebx                 ;n-1
  83.     MOV edx, dword 1        ;m= 1          
  84.     PUSH ebp
  85.     PUSH edx                ;push new m
  86.     PUSH ebx                ;push new n
  87.     CALL ackermann
  88.     RET
  89.  
  90. inputError:                 ;n or m was <0
  91.     MOV eax, promptError
  92.     CALL print_string
  93.     JMP end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement