|
|
本文成篇的起因于社长的一条记录 链接% w9 C5 @7 i2 T4 c0 _' ^$ Z" ?8 i
4 D7 E4 J* h, H6 A- b
递归是计算机科学中一种解决问题的重要方法。所谓递归,通俗地说就是自己调用自己,它在很多语言和环境下都可以实现。以社长的工作场景为例,根据爱坛上的信息,大概是两个关键词:Unix和汇编。本人条件有限,只能用Linux模拟,汇编也只限于x86汇编。下面就是用汇编语言编写的,用递归方法计算阶乘的示范程序。
7 \- H& ?) D/ |/ ]& o7 l+ ]" j- LEN_OF_OUTPUT equ 10
# C' r0 m& I9 P! d - 3 x% F! }6 S! V* a$ ^3 g) k# ~, r
- section .data ;% c" o( F/ H2 U
- userMsg db 'Please enter a single-digit number: ' ;3 F/ O$ b, M6 h
- lenUserMsg equ $-userMsg ;
, @% S" w( [# ^0 J. `; E7 Z" } - disp1Msg db 'The factorial of '( n7 a& C2 {7 ^; W; M) N
- lenDisp1Msg equ $-disp1Msg
b" G+ I! h, q! n - disp2Msg db ' is '
1 P4 \7 D+ K2 D0 j' v; \ - lenDisp2Msg equ $-disp2Msg $ i- D! g4 l1 M2 N/ l0 D. v' o
- errMsg db 'Wrong input. Only one-digit number can be accepted.',0x0a& M3 n8 G6 S" z, R
- lenErrMsg equ $-errMsg
, B# F/ O% ~; G2 w) \. Y - 9 q% R5 Y1 p ?( r1 [
- section .bss ;
4 z1 `5 d6 b2 |! `0 k - num resb 1
2 E4 u. q5 f; Q8 K ^. I - result resd 1
K* r- [! y. |# T# O - buffer resb LEN_OF_OUTPUT+17 G6 ~! ~ F9 \8 k( K I
- $ g8 s+ `8 y2 D& m- n
- section .text ;4 O$ s! x; s; N ?
- global _start
8 d: z1 U5 u& h1 ? -
! G" k2 } D4 v' m - _start: ; v: O. R+ W, I6 E2 V* U
- mov eax, 4
: N. `7 O9 n) T1 H - mov ebx, 18 U7 F& A) u) E& [
- mov ecx, userMsg# T V9 f& v' i5 G
- mov edx, lenUserMsg
$ c7 o" o& O) l+ ~! e F - int 80h
$ V { M* y/ e' q: V. _) | - 3 O8 ?3 v3 |- W+ `" V2 a" Z
- ; read user's input' o2 i- @- X$ I8 f/ C2 G
- mov eax, 3; q z7 M! d* ?- y
- mov ebx, 0
9 ^1 G7 d1 |# D$ f' [* h - mov ecx, num
2 S( A" Y- x# B! A - mov edx, 1 ; length of input buffer5 h* s1 U, j/ F g
- int 80h
* O. L* D) X7 [3 E; `* b -
! A; L6 g7 L8 [1 k3 _ - mov eax, [num]0 O3 U; U1 q, w7 I
- and eax, 0xFF
- |$ ?$ }5 `0 z - sub eax, 0x30
/ ]- s0 h T* M7 n9 h6 k - jl ERROR
) M( p Q( N2 C3 M v$ {+ v - cmp eax, 10
+ v" O3 T, u' E8 P4 D% u8 e0 b - jl DO_CALC( ]9 a4 i! X: @2 @4 {7 G) x' I7 m& X
- ERROR:
7 ~2 z$ `: R8 A - mov eax, 47 J1 H- T9 s$ W' s$ V
- mov ebx, 1
- I X- X" i! M- m% }6 m% I+ o& P - mov ecx, errMsg
9 R0 j$ @0 F% L - mov edx, lenErrMsg2 y2 n: s( I% h" w+ D: a4 w; c
- int 80h
) L- s c: U7 G - jmp EXIT! J# o7 q$ a7 A- y1 B" @
+ D! x+ H( y. f
3 y; }; N2 N$ H; B5 s) P+ I( O- DO_CALC:
p+ d y* K* g% v$ f7 i5 h( U - call factorial
5 c: m- |, C0 F, L/ ^0 @ - ; The parameter is in eax. After calling, the result will be stored in eax.$ y; a% @6 [$ }) P/ c$ D4 j: I0 I& Q$ G
- mov [result], eax ; Save the result" l" g5 `& N$ P% `: J, Y5 V
- 3 Q T3 C3 m* U4 H _" ~
- ; print result
) x8 `* \6 J# V g3 V6 v% u& f - mov eax, 4- S5 I2 V0 N' w: u/ Q( W3 \
- mov ebx, 1
( |# Z4 y: h* h8 v! D4 ] c" G3 k, A2 b - mov ecx, disp1Msg
6 b% z5 i" k4 c" Q9 z - mov edx, lenDisp1Msg
3 v; _0 s/ D0 ? F) f1 ] - int 80h
" V9 n- t3 [. F! d' E - mov eax, 4
6 c. P/ @4 p, M - mov ebx, 1, i1 J+ Q; t! e- ^) ?! x
- mov ecx, num/ e! G* B. `% p
- mov edx, 1* n$ `! O5 W+ v7 ]# A
- int 80h
! X+ `. U4 a- U - mov eax, 45 I1 l( Y2 b5 \; g
- mov ebx, 1( k& o x$ S* B% h& Y
- mov ecx, disp2Msg+ _2 N1 _! |) F7 g+ H) T
- mov edx, lenDisp2Msg
; U* P4 H; h6 p3 g1 Q& w: ? - int 80h
! k7 t m1 K* `5 `: L h* |( d
4 T! k# K2 o; x$ t6 H- ; output number
2 B% j) ]! @: z6 m - mov eax, [result] ; VALUE THAT I WANT TO CONVERT AND DISPLAY1 @! r9 H3 g) d$ n+ k
- mov byte [buffer+LEN_OF_OUTPUT],0x0a
4 ? p, l7 e6 l2 u4 f9 D' m' U - lea esi,[buffer+LEN_OF_OUTPUT]
$ P# a' E$ N( f( R$ |9 k - mov ebx,10 1 L1 b! l. n0 ~ D: F/ @
- ASC_LOOP:
- V1 l9 P/ i Y - mov edx,0 ; clear dx prior to dividing edx:eax by bx7 ^" x' I* s m7 s! ?
- div ebx ;DIV EAX/10
$ e. p6 @' d. B8 ^9 c$ A1 r - add edx,0x30 ;ADD 48 TO REMAINDER TO GET ASCII CHARACTER OF NUMBER
' j B6 w; ~' h. n - dec esi ; store characters in reverse order( p7 a8 G' P' a% M$ [
- mov [esi],dl: ?8 S+ P. x/ R( ^
- cmp eax,0 ! `0 _% {$ ~/ E& _# b
- jz END_ASC ;IF AX=0, END THE PROCEDURE OF NUMBER TO ASCII
$ N2 ~; X4 F$ @' \5 l - jmp ASC_LOOP ;ELSE REPEAT& _( q1 Y$ B1 x
- END_ASC:$ q1 j2 r4 s' i+ k
- mov eax, 4 b! s' V3 {1 e8 Y q
- mov ebx, 1
2 b# |! O8 \/ I) D% W - mov ecx, esi
: M! ?8 Y7 ?5 j - lea edx,[buffer+LEN_OF_OUTPUT+1]
* w- G# B; N: D* L" [1 W; Z - sub edx, ecx
- |& z( P. K9 [5 h; w) S) X, |# Z - int 80h * s8 O) ~: K# k* i* A
- X# M4 r ~/ @
- ; Exit code
: |$ y: u0 T5 [/ V6 l - EXIT:* r+ P+ P) ]9 I. F" y
- mov eax, 15 |& K t4 o7 }' I( F4 l6 b
- mov ebx, 09 ?8 C% W8 ]/ ]- K$ q0 m; m
- int 80h
% q4 T2 E5 C V) |9 @7 e
; T- K9 Z! L" _. b9 F; O
' ]: w5 ]8 \! p4 U# e& A( c- $ s0 M, k- q0 j4 w5 y. o
- ; Factorial function using recursion
. X" s( d7 Y2 ], h- H& \6 s' [2 V - factorial:
- Y% e( P8 t' b8 B2 v - ; Check if eax is 0 (base condition)
* a. c. T b# E- r - cmp eax, 0+ t0 W) ?; k' S7 Z' W, s# l
- jz end_recursion5 o+ ^/ l% A6 k# D) [
- & D4 h% G3 T/ ~
- ; Save the current value of eax" l: l0 B2 M* t8 ]) Y
- push eax
2 M$ F+ D$ n1 U; W3 H - - Y7 v$ R3 y, L0 j2 q! j& c
- ; Decrement eax and call factorial function recursively
3 f. {7 P: P5 I: v9 C& {: e - dec eax
; @! N2 [. P% J! P0 O - call factorial
) n, @: `# `$ @, ?" ?- H
# e( Z' h( ?8 k* m- ; Multiply the result returned in eax with the saved value of eax
1 x3 R3 ~0 @" ?) y* c8 n9 q1 J* M - pop ebx
' `! a' b1 U% j! U/ B- | @ - imul eax, ebx
% j+ C0 O5 ~2 b% Q0 [" ]* A* N - B7 C* h+ q: n
- ret
5 R$ i4 F3 E* \; q+ q4 o' k
6 ] D5 K; e0 p% k9 k7 a- end_recursion:
4 r$ M! ?+ _" B$ V0 O. M3 _# _ - ; Return 1 when eax is 0 (base condition)
! p, ?* J9 R* V - mov eax, 1
F, w$ q' k8 m5 Y2 a2 B' L7 A7 b - ret
复制代码
- l+ Y& O0 Y* \( a5 y, a# t程序在nasm编译器下通过并成功运行。相应的命令行如下,有兴趣的童鞋可以自行验证。, D) A7 W8 q$ I7 x! L# ~
- nasm -f elf factorial.asm% g/ I o4 `, b. }0 _ v
- ld -m elf_i386 -s -o factorial factorial.o4 T. `" M* n" Y- i
- ./factorial2 t- Y0 n3 n' j
复制代码 % l- {: q: i) M9 U1 _/ q
由于汇编不擅长处理I/O,所以程序限制输入仅限于一位数字(0-9),以免过于喧宾夺主。其实程序中真正紧扣主题的就最后一小段:
2 D1 I& l& A8 p- ; Factorial function using recursion
n; t4 }8 r0 x3 a - factorial:
0 s0 @* y4 N5 G' N% t) j! p! D - ; Check if eax is 0 (base condition)
6 C; X4 b" t, I* c3 N - cmp eax, 0& y1 a. N) a) B0 m! u6 C$ [
- jz end_recursion
$ R! h6 \3 ]* e2 i. I - & `) y) @* j8 D
- ; Save the current value of eax4 N% ]" Z, w3 S l1 U
- push eax
8 {# p6 @: }2 c2 S
' G2 S: E9 i& g# I E+ D- ; Decrement eax and call factorial function recursively
4 |9 x0 Z( A1 e- \- J - dec eax
* z3 d0 E d3 n3 T! x7 X - call factorial& b. ?6 x+ m7 J
0 E" l! w( x0 V. d6 W8 V2 B- ; Multiply the result returned in eax with the saved value of eax
0 w# M3 w& W* b0 o& C - pop ebx/ E& v9 b r! `$ ~! C6 I) r
- imul eax, ebx
p, C u# D0 ~: L
/ M7 E/ P3 C4 J- ret
) a' {( N% q2 [/ h - 9 j. u; I2 O. ^) P3 k; B
- end_recursion:
7 C J- e6 b, R3 L - ; Return 1 when eax is 0 (base condition)
, i) r) ^5 ?5 v7 Z. |8 Q5 A* U - mov eax, 1 q+ Z% ?# J% H' p0 I
- ret
复制代码 可以清晰看出函数在其函数体中调用了它本身。. Z# u% e0 D! @- l
" X' t8 m/ j0 g% k6 V0 t: y
以上证明了汇编语言不是实现递归的障碍。推而广之,所有中低级语言都可以轻松实现递归。某些高级语言反而不能做递归是人为强制规定的,换而言之是原作者权衡利弊的结果。5 \2 r O' Z. Z3 K: ^& R
; b. X6 F' j1 s; p& X5 D' o
世界上没有免费的午餐,递归在编写程序的时候简洁优雅,但运行过程中要付出极大的代价,甚至有可能是灾难。由于是函数反复调用自己,那么所有的中间结果都要暂时保留在栈(stack)上,直到最后一层函数调用完毕才能逐步清除。这既低效,又不安全。栈式计算是一个古老的概念,它与现代的多寄存器和流水线等都格格不入,难以优化提速。递归对栈空间的占用更可怕,占用量与调用函数次数线性相关(套用复杂度的概念就是O(n)),递归次数多了之后,很容易超出栈容量,从而导致栈溢出。具体到社长的工作中可能还有很多实时任务,一大堆高优先级的中断响应程序可不会等着递归函数运行完了再启动,这会让栈空间雪上加霜。想一想某个核电控制程序突然栈溢出了,那场面,哈哈哈。
8 x& f% @& `* p! f( v* T
6 s$ [7 I# N& c! c这可以解释,为什么条件上有可能,但社长很长时间里都没有接触到递归编程。无论用C还是用汇编,都强调的是速度与可靠性。社长实战出发,不需要知道回字的4种写法。至于Python,整个编程思想都变了,Python假定计算机的计算速度远远超过了需求,编程者无需为性能而操心。 Python语言的设计初衷,也不是为关键任务而生,而是想让更多的门外汉用起计算机来。所以在Python程序中容易见到递归。* h( L/ d/ d9 ?
& N$ t* M; ]1 E( O* Z9 b# V
有很多时候,递归用作概念性算法表达。真到了用代码实现的时候,再换用其它方法。比如说将递归转化成迭代(iteration)。* w$ b- s. z3 j: X
l, o# L8 Z5 m: A另外,汇编语言作为一种低级编程语言,在不考虑时间成本的前提下,没有什么是它做不了的。本期课后作业:如何用汇编语言实现继承(Inheritance)和多态(Polymorphism)?请写出示范程序。我们知道继承和多态是面向对象编程(Object-oriented programming)的两个基本点,有了它们就可以实现汇编语言的面向对象编程。+ C: s5 k+ `9 @! V, {, A7 v
5 l, z1 j/ u8 }: o) J3 U* k3 N
|
评分
-
查看全部评分
|