首页 > 代码库 > 汇编语言-整型处理,利用堆栈原样输出

汇编语言-整型处理,利用堆栈原样输出

要求:输入任意一个整型数字字符串,并将整型原样输出。

这个子程序主要用于格式化排版,比output直接输出字符串有很大美观性。

 1 ; Example assembly language program --
 2 ; Author: Karllne
 3 ; Date:    revised 05/2014
 4 
 5 .386
 6 .MODEL FLAT
 7 
 8 ExitProcess PROTO NEAR32 stdcall, dwExitCode:DWORD
 9 
10 INCLUDE io.h            ; header file for input/output
11 
12 cr      EQU     0dh     ; carriage return character
13 Lf      EQU     0ah     ; line feed
14 
15 .STACK  4096            ; reserve 4096-byte stack
16         
17        
18         
19 .DATA                   ; reserve storage for data
20 
21         prompt  BYTE  "Please enter a number",cr,Lf,0
22         fl      DWORD ?
23         value   BYTE 11 DUP(?),0
24         char    BYTE 1 DUP(?)
25         
26 PUBLIC _start        
27 .CODE                           ; start of main program code
28 _start:
29         output prompt
30         input  value,11
31         atod   value
32         push eax
33         call byteInput
34         add  esp,4
35         
36         INVOKE  ExitProcess, 0  ; exit with return code 0
37  
38 byteInput PROC NEAR32
39           push ebp
40           mov  ebp,esp
41           
42           mov  fl,0
43           mov  ebx,10
44           mov  eax,[ebp+8]
45           doWh:
46               cdq
47               idiv ebx
48               push edx
49               inc fl
50               cmp eax,0
51               je  enddoWh
52               
53              
54               jmp doWh
55           enddoWh: 
56           
57           doPrint:
58                  
59                  cmp fl,0
60                  je  enddoPrint
61                  dec fl
62                  pop edx 
63                  mov char,dl
64                  add char,0
65                  output char
66                  jmp doPrint
67           enddoPrint:
68           pop ebp
69           ret
70           
71 byteInput ENDP
72 
73 END                             ; end of source code


测试结果: