輸一整數 n, 印出 其絕對值。 by gnu assembly

 1 #Program Description:
 2 #Author:Jeremy
 3 #Creation Date:2012/9/25 10:21
 4 #Revisions:
 5 #Date:     #Modified by:
 6 
 7 #寫一個組合語言程式,輸一整數 n, 印出 其絕對值。
 8 .section .data
 9    msg: .asciz "請輸入一整數:"
10    n:   .int 0
11    ifmt:.asciz "%d"
12    ofmt:.asciz "the abs value is %d"
13 .section .text
14     .globl _main
15 _main:
16     pushl $msg
17     call _printf 
18     addl $4, %esp
19 
20     pushl $n
21     pushl $ifmt
22     call _scanf    # scanf("%d",&n)
23     addl  $8, %esp
24    
25     cmp $0, n         # 數值一定要在前面
26     jge postive       # n >= 0 ?
27     movl $0, %eax
28     subl n, %eax      # subl 目標欄位必為register
29     pushl %eax
30     pushl $ofmt
31     call _printf
32     addl $4, %esp
33     jmp quit
34 postive:
35     pushl n     #printf("%d",n)
36     pushl $ofmt
37     call _printf
38     addl $4, %esp
39 quit:
40     pushl $0
41     call _exit
42     
posted @ 2012-09-26 15:14  jeremyatchina  阅读(1003)  评论(0编辑  收藏  举报