|
本文成篇的起因于社长的一条记录 链接
" J/ j7 n, B' e1 `+ t* V- z" Q& }- ~3 N1 q0 U( e5 O, U
递归是计算机科学中一种解决问题的重要方法。所谓递归,通俗地说就是自己调用自己,它在很多语言和环境下都可以实现。以社长的工作场景为例,根据爱坛上的信息,大概是两个关键词:Unix和汇编。本人条件有限,只能用Linux模拟,汇编也只限于x86汇编。下面就是用汇编语言编写的,用递归方法计算阶乘的示范程序。0 U9 |7 p9 N# s# n8 g# H
- LEN_OF_OUTPUT equ 10
' G, Q* c6 s k% b1 i' O
2 ?2 H, J9 w+ X* s1 g# J# O- section .data ;% t' W7 @: b$ o2 j5 F
- userMsg db 'Please enter a single-digit number: ' ;" T2 p5 ^ g: e- f3 y3 @
- lenUserMsg equ $-userMsg ;
% n+ [/ h; K' p' O, O - disp1Msg db 'The factorial of '
^" j+ O8 T6 [9 o3 @- M8 a. [ - lenDisp1Msg equ $-disp1Msg
' P& s- D) C; W3 z1 V - disp2Msg db ' is '+ {8 |( N& C$ q R' o
- lenDisp2Msg equ $-disp2Msg $ C2 W. V4 ~9 I6 z$ F
- errMsg db 'Wrong input. Only one-digit number can be accepted.',0x0a
; e# J& R. g4 Z6 k - lenErrMsg equ $-errMsg
, `) q9 U8 W$ M1 ` - P* b" R- ?" Z
- section .bss ;! A3 z7 Z4 n9 T# @! k
- num resb 13 j* v+ Z( U) N- t" A$ \" A
- result resd 1; N3 a9 C$ G2 Q
- buffer resb LEN_OF_OUTPUT+1. ^2 a! F" D9 _2 r2 n A
- $ S0 H* ?6 ?' D5 a3 z
- section .text ;8 Z3 @7 A* `9 T8 R; c( E0 @. n
- global _start4 ^ A4 I$ N4 J7 W' o: _8 A
- % n' V) T2 Q7 f, ~/ D# ]8 [
- _start: ;
; v& p# ]6 V( o! a* A - mov eax, 4) f" k# J+ P2 K# D( Y; t+ F' r3 s3 T
- mov ebx, 1
/ T I: D8 v$ h1 g. i% s, W: Q3 i - mov ecx, userMsg
# d( z% z3 Z3 ^+ g2 p! n' } - mov edx, lenUserMsg
h v; d( f; r* C1 d - int 80h5 T- \& T( w- V4 a
* U! _. W- Q! t' O0 o+ ~- ; read user's input
3 r9 s' t: q! f t8 \! G - mov eax, 3! o$ K7 v* w- r9 I0 K7 o$ W
- mov ebx, 0! b' I% r; I+ N% @1 T! P
- mov ecx, num
+ E. W. T8 M% {# C+ Z$ s3 | - mov edx, 1 ; length of input buffer
' Q$ u5 ~+ K- A. U - int 80h
/ _; S8 @7 E, m+ n5 P -
3 H3 j$ f; |2 K - mov eax, [num]
* r; j u9 r) L, U - and eax, 0xFF
0 r9 ^1 j8 E5 M! x5 T# a' E - sub eax, 0x30
0 `. P8 a. M% p6 ]0 @9 t - jl ERROR8 H2 H- _! U1 J" K8 c
- cmp eax, 103 `+ q+ \# [/ _! Q3 ?4 l7 R( t
- jl DO_CALC; W9 ]' h+ k( R% S% T: P+ u
- ERROR:
) E& ]# l J! \3 S/ [$ T7 F - mov eax, 4
/ k1 d; | f6 s/ M - mov ebx, 1
" D+ p6 s; n; U+ b( z" z( u; O' \ - mov ecx, errMsg
* _8 `3 i% `5 d/ d3 j - mov edx, lenErrMsg
/ W) p- e& c; r! {. p$ F7 Q3 K) @ - int 80h
4 l; `; a: m* C+ o! z& v - jmp EXIT
9 G: B7 y$ t9 k - + B$ a: B1 x; t7 X
- . s1 ?+ D- u& H+ R8 U) z8 l
- DO_CALC:: G* C$ O- x! a% |' A
- call factorial$ J6 l j+ M' l( W) P' ~ d
- ; The parameter is in eax. After calling, the result will be stored in eax.
3 K7 l5 c) S N: U4 a& f- i* K" u - mov [result], eax ; Save the result+ D- b" d# p; k& n2 [
- 6 l0 T) `* i' `9 |
- ; print result0 J% H' G! k0 O/ u: C3 J
- mov eax, 4 H- c2 y4 ?$ ?: k3 r
- mov ebx, 1
( R: [' @8 X1 k$ ]/ o" _" k1 w, _ - mov ecx, disp1Msg
3 u ]0 a0 u% {5 {1 P' a+ l8 X$ |$ u - mov edx, lenDisp1Msg$ [; C0 v( b( z/ p$ c4 C
- int 80h 9 f5 U0 e* [3 w, g9 v7 S/ K- a
- mov eax, 49 N/ e5 P: l5 j# D! U7 B1 c/ x
- mov ebx, 13 o2 {- n7 K# |# n7 w/ T$ p0 }
- mov ecx, num
# _* P5 Q+ e9 [6 }# h. C - mov edx, 1
: D9 s9 V7 g' @# _6 s; s) S; [' q - int 80h
: Q% K8 K: j0 [- x9 S' A& j+ ] - mov eax, 42 \2 a5 s/ m& M' c7 j; K6 L
- mov ebx, 1
9 s4 P6 |& `: I; u( m- l2 E$ O5 [ - mov ecx, disp2Msg9 w5 L0 y8 @2 a- V2 h
- mov edx, lenDisp2Msg
/ m% K' e5 @+ Z, a+ A' J' I - int 80h 1 |0 V' |" _% I" q1 j6 @1 s- o
- 8 V! V2 W- u; e2 e% g. U& z; G
- ; output number5 P& K3 U5 c6 N w
- mov eax, [result] ; VALUE THAT I WANT TO CONVERT AND DISPLAY+ ~+ _2 ?- _& B% X, j5 {7 Q
- mov byte [buffer+LEN_OF_OUTPUT],0x0a
9 T7 J' q3 V1 V8 _ - lea esi,[buffer+LEN_OF_OUTPUT]8 U* l. A3 @. ?
- mov ebx,10 5 x6 V0 g9 d- }
- ASC_LOOP:
& X$ ]6 @- o/ `- y - mov edx,0 ; clear dx prior to dividing edx:eax by bx2 I, C% q8 v& D2 U4 O$ U7 j
- div ebx ;DIV EAX/10
$ c u; K; c* L- L! ?6 F - add edx,0x30 ;ADD 48 TO REMAINDER TO GET ASCII CHARACTER OF NUMBER 3 k' M4 R( ?% H! c( p. M+ J
- dec esi ; store characters in reverse order
: [/ | W% O& d - mov [esi],dl& ~* P- ~ \4 z. r& s
- cmp eax,0 # E; k; g5 D E; a
- jz END_ASC ;IF AX=0, END THE PROCEDURE OF NUMBER TO ASCII$ `. r+ k, j5 Y# ]
- jmp ASC_LOOP ;ELSE REPEAT3 _7 \: C0 a! M3 A5 i
- END_ASC:
5 b" X2 `- D. s' _1 { - mov eax, 4" j8 O s! S$ A( ^
- mov ebx, 1% m' }5 Q; i' Z! x! Z. `' a+ O: h, p
- mov ecx, esi% T- J2 Y8 H- F2 ~( y P/ t
- lea edx,[buffer+LEN_OF_OUTPUT+1]
( x' H& }: k+ h+ P# K, ~# ~ - sub edx, ecx# G0 Z# a% F. J% O( h
- int 80h * b* F" @1 @0 J+ c2 l
-
6 z1 _# D- T7 w; y - ; Exit code
# b- t' W( L1 w1 i# N: ]1 @ - EXIT:) q+ _- O. x/ f% A% \: y
- mov eax, 1* z! [$ b5 ^9 R7 A
- mov ebx, 0! `! {2 [3 a2 Z4 e
- int 80h6 n. V! n- A9 U8 B. A7 W" ^
. ?+ K3 ~2 y+ ], Y; ]
$ i9 Y( f0 ^( Y/ w; B- ! ^; H/ m9 l7 _" I' F
- ; Factorial function using recursion1 n% `4 }; U/ H, S: r
- factorial:5 k% ]* X& d9 h5 Z
- ; Check if eax is 0 (base condition) R, d! I* G0 P
- cmp eax, 09 C# i9 \, G, K, H* c/ n
- jz end_recursion" A' Q4 [' e/ g) Y. f4 M& _2 S
. x; h+ T# s, ?8 {- ; Save the current value of eax2 ]& t% S9 |1 \: S5 {
- push eax
# |2 e! }! w' m) R - . N, ?, j& o) G
- ; Decrement eax and call factorial function recursively
2 I+ f7 C5 m4 [- d |. R2 c& O - dec eax9 |& x$ K0 U, _* Z. U% q
- call factorial$ [( E+ }8 b- @/ _2 ^) H) f# {+ _
' \* g/ _1 y( W! V- ; Multiply the result returned in eax with the saved value of eax
+ l+ g+ V+ h a( R' F - pop ebx, l5 D {" L. P( F8 a+ s( S
- imul eax, ebx
* Y# S, k& q+ \' u2 w4 `* v/ }2 Y& b
1 L/ F5 p; i+ v3 d& p( l& V- ret! w) T) @0 n) v) e" r, h
- 3 Y3 R. P& t9 U6 e
- end_recursion:
7 \; d& `; `1 l. x! ~4 c0 n% b0 { - ; Return 1 when eax is 0 (base condition)
+ f% T% {, J$ V$ d% w; ` - mov eax, 1
! V7 q7 L5 i" _: d! D) k3 p - ret
复制代码 5 f9 E4 A/ k$ f# e: H( H1 f
程序在nasm编译器下通过并成功运行。相应的命令行如下,有兴趣的童鞋可以自行验证。
- P7 M1 [2 v& s7 j- g% y' ^& _- nasm -f elf factorial.asm
; O# o+ T4 H; X! i5 O. F - ld -m elf_i386 -s -o factorial factorial.o' n' G* o- `1 G; H
- ./factorial5 ]9 J/ w0 T& l. J# q
复制代码
1 Y- C3 f" j' l5 a) I: j" f' T由于汇编不擅长处理I/O,所以程序限制输入仅限于一位数字(0-9),以免过于喧宾夺主。其实程序中真正紧扣主题的就最后一小段:$ p" Q: X# Q$ B0 \! Q! K3 r" K
- ; Factorial function using recursion1 t% i2 I9 Z% V% w% b
- factorial:2 j% l/ S) \/ G/ ~6 S# V
- ; Check if eax is 0 (base condition)
: e$ z$ U% V4 @! k7 p - cmp eax, 0
, m3 j3 r5 f4 T8 v5 \ - jz end_recursion
0 r5 {. j1 y: R& E2 X - ; \, z6 @; Z6 t/ ]$ `, e. c
- ; Save the current value of eax& _9 c; p* u9 M: ~% G) M
- push eax2 f2 H1 e0 @$ j7 ^% t
! P: @4 R9 M; g- ; Decrement eax and call factorial function recursively
2 ?# k+ T y9 _ - dec eax2 i* b! [0 Z7 T' Y
- call factorial" D& U% B7 z# ~) c$ P
- }$ k, C7 a: E2 ]( W- ; Multiply the result returned in eax with the saved value of eax _+ O6 Y3 f% l9 j1 |. S8 x
- pop ebx
7 Q2 ~3 D/ E* |' y* s( q - imul eax, ebx: S' Y' e! A9 ~1 K2 C. w3 P
- : n" y# G P9 L, s- x6 r$ t
- ret
4 E! w" ?2 U5 H) U1 A# C - d+ a! b- y# K
- end_recursion:% G. \# H: D+ V. E0 N7 E4 H8 S
- ; Return 1 when eax is 0 (base condition)
2 i6 A/ @$ W0 c/ R - mov eax, 1
: P/ b6 w5 x' ]4 f - ret
复制代码 可以清晰看出函数在其函数体中调用了它本身。
7 V+ `$ _$ ~; F6 @2 ]6 ]& G- u3 ?8 d
4 P" {/ @ G! G5 j; }' f. C以上证明了汇编语言不是实现递归的障碍。推而广之,所有中低级语言都可以轻松实现递归。某些高级语言反而不能做递归是人为强制规定的,换而言之是原作者权衡利弊的结果。
- t" K; J+ m M
. A d, C2 s* J9 p0 g: |, v世界上没有免费的午餐,递归在编写程序的时候简洁优雅,但运行过程中要付出极大的代价,甚至有可能是灾难。由于是函数反复调用自己,那么所有的中间结果都要暂时保留在栈(stack)上,直到最后一层函数调用完毕才能逐步清除。这既低效,又不安全。栈式计算是一个古老的概念,它与现代的多寄存器和流水线等都格格不入,难以优化提速。递归对栈空间的占用更可怕,占用量与调用函数次数线性相关(套用复杂度的概念就是O(n)),递归次数多了之后,很容易超出栈容量,从而导致栈溢出。具体到社长的工作中可能还有很多实时任务,一大堆高优先级的中断响应程序可不会等着递归函数运行完了再启动,这会让栈空间雪上加霜。想一想某个核电控制程序突然栈溢出了,那场面,哈哈哈。9 u& M. T! _$ Q- ]
9 s' G9 [# l& e X0 P3 ~ b$ x这可以解释,为什么条件上有可能,但社长很长时间里都没有接触到递归编程。无论用C还是用汇编,都强调的是速度与可靠性。社长实战出发,不需要知道回字的4种写法。至于Python,整个编程思想都变了,Python假定计算机的计算速度远远超过了需求,编程者无需为性能而操心。 Python语言的设计初衷,也不是为关键任务而生,而是想让更多的门外汉用起计算机来。所以在Python程序中容易见到递归。
0 J3 V9 @$ k- l2 w2 W/ r4 A3 |6 M/ N* @( \) l3 G/ j
有很多时候,递归用作概念性算法表达。真到了用代码实现的时候,再换用其它方法。比如说将递归转化成迭代(iteration)。4 p1 H$ s$ e$ i# h
" t! _0 N) E& ^) p' K( i' [
另外,汇编语言作为一种低级编程语言,在不考虑时间成本的前提下,没有什么是它做不了的。本期课后作业:如何用汇编语言实现继承(Inheritance)和多态(Polymorphism)?请写出示范程序。我们知道继承和多态是面向对象编程(Object-oriented programming)的两个基本点,有了它们就可以实现汇编语言的面向对象编程。; H& @! V0 }7 ?9 x( c
* D4 g- y3 [. ~
|
评分
-
查看全部评分
|