|
|
本文成篇的起因于社长的一条记录 链接/ h! g2 }( z5 B5 ~6 h
* U c# f+ ^- `( [递归是计算机科学中一种解决问题的重要方法。所谓递归,通俗地说就是自己调用自己,它在很多语言和环境下都可以实现。以社长的工作场景为例,根据爱坛上的信息,大概是两个关键词:Unix和汇编。本人条件有限,只能用Linux模拟,汇编也只限于x86汇编。下面就是用汇编语言编写的,用递归方法计算阶乘的示范程序。
" u2 f5 V- B" v8 W+ V; |8 q- LEN_OF_OUTPUT equ 102 }( t0 c J5 U( ~
- ' |/ t: p+ u7 i4 H m
- section .data ;
- z/ x+ m+ H: ?# o' X - userMsg db 'Please enter a single-digit number: ' ;
$ ~4 L5 i3 {6 X: G, n. s - lenUserMsg equ $-userMsg ;
1 {2 k# @+ m/ ]5 i$ j+ c6 z - disp1Msg db 'The factorial of '
, \1 \; {' d1 v5 Z3 [3 }0 b9 L - lenDisp1Msg equ $-disp1Msg
9 }# C5 z7 X2 l2 k( W - disp2Msg db ' is '/ i% [; g5 ^3 S( g6 h
- lenDisp2Msg equ $-disp2Msg 8 i c i% K& A" ]- C; S% X1 d
- errMsg db 'Wrong input. Only one-digit number can be accepted.',0x0a1 P! a1 y; \ S; S% ?
- lenErrMsg equ $-errMsg
. ?- q3 f; {, [
/ f/ m5 r4 o! Y: G3 j1 Q; d" d- section .bss ;
% g& [: i* u7 a" n - num resb 14 D/ Y4 n3 K7 h! F# R9 S g" X& M
- result resd 17 _; |; t7 F; r9 D4 _% k/ H
- buffer resb LEN_OF_OUTPUT+1% z& U( x2 L y7 |. O2 n4 ^
- $ I* s+ Z7 I3 T# ], d" U
- section .text ;
r. x$ o; B$ ?0 ?1 Y2 R& A - global _start
& b$ p# [& \/ U B$ U. u& t - * p/ H" s. q }/ s1 B1 r: b
- _start: ;
+ y1 B, a6 ^- d; n9 i1 N - mov eax, 4
: O- d( ?, b5 S - mov ebx, 1
! ^7 b* V! f* f3 q$ D: [/ N- e - mov ecx, userMsg
1 h# {& l9 d5 k/ n ? - mov edx, lenUserMsg
- P: T* q2 E0 I( {- w - int 80h
7 p, K4 `& k4 ?2 H2 c2 c( }$ ]2 p/ J: A
9 [4 m1 M) U% c0 n" T- ; read user's input4 M2 B) m% q) O% P$ l
- mov eax, 3
5 e& Y( g3 T# W/ _- d - mov ebx, 0* j6 n X& g$ n# H8 I
- mov ecx, num
@: J' J0 m) ? - mov edx, 1 ; length of input buffer
; x, n5 m# V9 I# P2 A: q - int 80h
7 E* E% l! \( M c. l4 K7 s0 _" B2 U - - _( i g {! x
- mov eax, [num]
, u/ L! n0 W$ y' { - and eax, 0xFF
3 |% E: n) x0 i - sub eax, 0x30
& ] T! k0 ^. ^$ q, ]( m5 Q M - jl ERROR7 ?* a- S0 O2 P2 @# y
- cmp eax, 10
- w0 {# K2 P8 n: e9 G; z - jl DO_CALC. p* M( I3 Q3 C$ ~0 u$ Q+ j
- ERROR:
: ~) |7 e! I* {# F - mov eax, 4% w- @0 [" D! T( |4 C
- mov ebx, 18 |( M! C' G% s+ S
- mov ecx, errMsg
* K: Y+ D0 d6 F - mov edx, lenErrMsg
1 O. T# q4 ` m# m; R - int 80h 5 s6 e6 ]4 o+ _6 l* M( J
- jmp EXIT
- F( q1 n+ x7 d- T) z& ?' Y& t
; j& e! v" f7 K7 x# @$ [. v8 ^/ l4 a2 |- 2 @% y+ o. `: W. |# [
- DO_CALC:
1 b% [9 `+ ?& ?- q& p - call factorial- c4 }# h. U& s. J) k7 i
- ; The parameter is in eax. After calling, the result will be stored in eax.
5 t O' w/ u6 V/ f- o) G - mov [result], eax ; Save the result+ o+ G# C# i6 ]" S
- / K( e5 T' O9 y" a3 s- o0 i$ Q
- ; print result
! U1 f6 S2 \) c2 e$ h - mov eax, 4
( l9 ^: L# ^: K, | t4 v7 d1 }4 @) y - mov ebx, 1
1 M/ s2 `3 r9 t* n - mov ecx, disp1Msg& I! G0 k K# e# c O
- mov edx, lenDisp1Msg& y( q: U0 \1 p: U: n6 b; T
- int 80h
2 }0 `: B( A1 D" N" _7 d, c - mov eax, 4- V6 H1 X" G6 b5 w* V7 ~8 z* \
- mov ebx, 1
8 d/ r( d+ y# d6 g+ L; \+ @ - mov ecx, num N6 Z; x9 R' ` j( t
- mov edx, 1
2 M- d% l, H6 v' ` - int 80h
2 P+ X/ E) v% O! X9 I4 |7 V0 }' B1 H - mov eax, 42 C# P9 g: Q j: L: r
- mov ebx, 1
- u* t, b# C# }8 R - mov ecx, disp2Msg: T4 [# f( B0 H+ o5 X" J$ d
- mov edx, lenDisp2Msg: j0 H( r8 Z0 G1 o6 k
- int 80h
3 y" u/ V( ~0 @* f1 I0 M - : _2 T" ?3 _" R
- ; output number7 \& a6 G3 M4 Z% n" _5 L, @8 B
- mov eax, [result] ; VALUE THAT I WANT TO CONVERT AND DISPLAY0 X' s+ E( l7 Y
- mov byte [buffer+LEN_OF_OUTPUT],0x0a3 C4 z6 V5 k1 D1 L- f" r
- lea esi,[buffer+LEN_OF_OUTPUT]
D3 J0 n3 n* o - mov ebx,10 3 b3 q' q2 ]. E5 C8 \5 ~/ B
- ASC_LOOP:# ^. M- o9 ~. F1 c4 |
- mov edx,0 ; clear dx prior to dividing edx:eax by bx
7 D7 k# J, l" A4 w - div ebx ;DIV EAX/10
: s% d4 |& b8 v# e0 N2 z* ~& G - add edx,0x30 ;ADD 48 TO REMAINDER TO GET ASCII CHARACTER OF NUMBER 8 E) O/ t) r" O( _( n+ v, I) Q
- dec esi ; store characters in reverse order; k# C% c* \7 E; `2 G, x
- mov [esi],dl
# o, ^4 y) D1 E: @ - cmp eax,0
: r0 Z! ^1 z$ A% `2 b- l - jz END_ASC ;IF AX=0, END THE PROCEDURE OF NUMBER TO ASCII m( U4 L; g& \2 {' h5 T
- jmp ASC_LOOP ;ELSE REPEAT: ~( z1 L7 j# w+ i* q
- END_ASC:
4 M* Y7 s+ _5 n/ S4 ~6 B - mov eax, 4; e# g0 ^) t- L% U' A: w
- mov ebx, 1
6 `7 i. o# k- w5 a& | - mov ecx, esi
: k1 Z' p$ D! o& G, F; l/ M - lea edx,[buffer+LEN_OF_OUTPUT+1]: L, a; X0 D0 `4 h h) Q
- sub edx, ecx
. J O" c: ]1 K* Q! F - int 80h 3 l( p/ | e, K! t; b+ ?4 Y
- b4 B: e* l: d% h# L' f
- ; Exit code
8 G ^9 M5 w m - EXIT:) N2 s7 B$ |- h" W3 L
- mov eax, 1
0 c8 L. L* \, X8 f' l$ |5 S - mov ebx, 0
& G4 L, ^8 o3 `6 p - int 80h) z& T) N& [6 u
- 8 }; q! l2 ^; P1 a3 P6 m+ [% {
- 2 m- M$ G, z8 [6 d) w
' [4 _7 Q% w3 X- ; Factorial function using recursion! o& G( n. ]' p* y8 P/ ?' Z/ ?
- factorial:
. y+ K. f' {7 S+ f0 B - ; Check if eax is 0 (base condition)
0 S. U' V5 _2 t - cmp eax, 0" v @/ l, n! u* R! M! Z
- jz end_recursion, B$ U- u/ L9 }- k- j
4 M5 i$ S1 D; a7 ]5 V8 E0 u; _- ; Save the current value of eax
: U5 h8 u+ h* `/ u' t - push eax# A: s' K, R" a% ]
- ! i% C% h) S4 x% b
- ; Decrement eax and call factorial function recursively/ N; R* a, k. B( O% }, i! Z1 Q
- dec eax
8 g8 f' _+ R$ Y# a+ C - call factorial1 e5 d/ L: c/ |# c+ N P( [; D
- , F( j% ^7 Y* s
- ; Multiply the result returned in eax with the saved value of eax" s2 Z2 U [- j/ E/ f- W" w. g
- pop ebx
. Y+ [% \% X, ^) w0 Y) z7 e - imul eax, ebx
% L4 `/ P7 I x9 k# D) L n; [4 {
1 ]% p) }0 u s- ret& \! O0 ^) a* o
- 1 f. v% M9 K9 g; c6 D+ X; W
- end_recursion:" Y% O/ ~ F1 n, D) A
- ; Return 1 when eax is 0 (base condition)8 d( E* ?4 y* c& G% [
- mov eax, 1; y! i9 j% v, ~* f5 g" n
- ret
复制代码 8 l' x) V( G$ n
程序在nasm编译器下通过并成功运行。相应的命令行如下,有兴趣的童鞋可以自行验证。
. B: }! @, P) _; D8 E- nasm -f elf factorial.asm
5 i+ z% |6 F+ v: v0 G( D0 t - ld -m elf_i386 -s -o factorial factorial.o! J h. Z& s* H% W7 h% F
- ./factorial
9 ^+ G+ U) |3 a5 T( J' d
复制代码 ) x" a6 W# Z8 M; P- J
由于汇编不擅长处理I/O,所以程序限制输入仅限于一位数字(0-9),以免过于喧宾夺主。其实程序中真正紧扣主题的就最后一小段:& l4 Y( a) T2 z+ X9 o
- ; Factorial function using recursion
: v$ O+ P, G& c! r - factorial:
" I% ?( o& q( l: _0 d5 y5 ]: ] - ; Check if eax is 0 (base condition)
- Z9 S8 U1 K. p) V5 O9 D: Q - cmp eax, 0
0 O) [$ U$ V4 C( z5 a - jz end_recursion) n f+ ]1 f" n( T2 ^2 y
1 j5 f% v3 B0 D. |7 @- ; Save the current value of eax7 S4 ?/ C! _* i( o$ K/ L$ j9 M( a8 z
- push eax
" g1 f. ]; p$ y5 O | - / w6 k" H6 F3 p7 m
- ; Decrement eax and call factorial function recursively
- n% ^0 }8 Y2 D- q. ]6 A3 K; B - dec eax
4 O) \& y) f' C# \3 j - call factorial! ` a: j* _: D9 M+ X+ x& H
- 8 E1 t7 J# x3 h. i1 A
- ; Multiply the result returned in eax with the saved value of eax
% z- _0 }4 A1 x$ c - pop ebx0 o* B1 U! ]+ ^( _2 I+ X+ \
- imul eax, ebx
3 S8 m; e, l6 U( C; C0 q - ; K" c' F& h/ c/ P; [0 Y
- ret+ y/ I" |- L1 p* m
" J" P1 l7 e. u# L& J% }, c- end_recursion:- [% A3 U* z' |: ] ]1 k9 Y
- ; Return 1 when eax is 0 (base condition)# M2 i; N6 s& ^# E3 h1 n
- mov eax, 1
/ w2 F; V! k. j( Q1 D+ c - ret
复制代码 可以清晰看出函数在其函数体中调用了它本身。
) T2 b3 v, K6 q. g6 E m7 T3 J% ?
以上证明了汇编语言不是实现递归的障碍。推而广之,所有中低级语言都可以轻松实现递归。某些高级语言反而不能做递归是人为强制规定的,换而言之是原作者权衡利弊的结果。$ s% h' K- J/ Z; F ]+ E5 ]8 O! {
. q0 J. K9 J4 m
世界上没有免费的午餐,递归在编写程序的时候简洁优雅,但运行过程中要付出极大的代价,甚至有可能是灾难。由于是函数反复调用自己,那么所有的中间结果都要暂时保留在栈(stack)上,直到最后一层函数调用完毕才能逐步清除。这既低效,又不安全。栈式计算是一个古老的概念,它与现代的多寄存器和流水线等都格格不入,难以优化提速。递归对栈空间的占用更可怕,占用量与调用函数次数线性相关(套用复杂度的概念就是O(n)),递归次数多了之后,很容易超出栈容量,从而导致栈溢出。具体到社长的工作中可能还有很多实时任务,一大堆高优先级的中断响应程序可不会等着递归函数运行完了再启动,这会让栈空间雪上加霜。想一想某个核电控制程序突然栈溢出了,那场面,哈哈哈。
- U: T4 f8 ~' U- R% l P9 Z3 Q- W" O0 n" p- H6 P
这可以解释,为什么条件上有可能,但社长很长时间里都没有接触到递归编程。无论用C还是用汇编,都强调的是速度与可靠性。社长实战出发,不需要知道回字的4种写法。至于Python,整个编程思想都变了,Python假定计算机的计算速度远远超过了需求,编程者无需为性能而操心。 Python语言的设计初衷,也不是为关键任务而生,而是想让更多的门外汉用起计算机来。所以在Python程序中容易见到递归。
/ |9 c6 E$ v. w' k' T
y5 |: E }8 g Q/ X有很多时候,递归用作概念性算法表达。真到了用代码实现的时候,再换用其它方法。比如说将递归转化成迭代(iteration)。
+ [ \5 G4 g- K- Z1 l" Q2 \$ c+ ~9 p$ n$ Q# l
另外,汇编语言作为一种低级编程语言,在不考虑时间成本的前提下,没有什么是它做不了的。本期课后作业:如何用汇编语言实现继承(Inheritance)和多态(Polymorphism)?请写出示范程序。我们知道继承和多态是面向对象编程(Object-oriented programming)的两个基本点,有了它们就可以实现汇编语言的面向对象编程。3 V ~6 G+ ~5 _- V4 A2 f. u8 ~
7 |1 A" h9 T# V Y- _
|
评分
-
查看全部评分
|