Advertisement
Guest User

Untitled

a guest
Jun 3rd, 2011
829
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 ebp
  41.     PUSH edx                ;push m
  42.     PUSH ebx                ;push n
  43.     CALL ackermann
  44.     POP ebx
  45.     POP edx
  46.     POP ebp
  47.     JMP result
  48.  
  49. ackermann:
  50.     MOV edx, [ebp+8]        ;get m
  51.     MOV ebx, [ebp+12]       ;get n
  52.     CMP ebx, 0
  53.     JE firstcase
  54.     CMP edx, 0
  55.     JE secondcase
  56.     ;inner ackermann return for m
  57.     DEC edx
  58.     PUSH ebp
  59.     PUSH edx
  60.     PUSH ebx
  61.     CALL ackermann
  62.     POP ebx
  63.     POP edx
  64.     POP ebp
  65.     DEC ebx
  66.     PUSH ebp
  67.     PUSH edx                ;return from last call is m
  68.     PUSH ebx
  69.     CALL ackermann
  70.     POP ebx
  71.     POP edx
  72.     POP ebp
  73.     RET
  74.    
  75. firstcase:
  76.     MOV eax, debug
  77.     CALL print_string
  78.     MOV ecx, edx
  79.     INC ecx
  80.     RET
  81.    
  82. secondcase:
  83.     DEC ebx
  84.     PUSH ebp
  85.     PUSH 1                  ;push m
  86.     PUSH ebx                ;push n
  87.     CALL ackermann
  88.     POP ebx
  89.     POP edx
  90.     POP ebp
  91.     RET
  92.    
  93. result:
  94.     MOV eax, prompt3
  95.     CALL print_string       ;print result
  96.     MOV eax, ecx
  97.     CALL print_int
  98.     CALL print_nl
  99.    
  100. end:
  101.     POPA
  102.     MOV eax, 0              ;return Value for C-driver
  103.     LEAVE
  104.     RET
  105.  
  106. inputError:                 ;n or m was <0
  107.     MOV eax, promptError
  108.     CALL print_string
  109.     JMP end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement