Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 83 lines (58 sloc) 1.307 kb
54f7bb5 @dharmatech add compare-ints
authored
1
2 # Compare two ints
3
4 # C
5
6 ```c
7
8 #include <stdio.h>
9 #include <stdlib.h>
10
11 int main ( int argc , char * argv[] )
12 {
13 int a ;
14 int b ;
15
16 a = atoi( argv[1] ) ;
17 b = atoi( argv[2] ) ;
18
19 if ( a > b )
20 printf( "greater than\n" ) ;
21 else if ( a < b )
22 printf ( "less than\n" ) ;
23 else
24 printf ( "equal\n" ) ;
25
26 return 0 ;
27 }
28 ```
29
30 # Assembly
31
32 ```s
33
34 .include "utils.s"
35
36 global_text main
37
38 prepare_stack
39
40 .set offset , 0
41
42 stack_allocate ARGV , 8
43 stack_allocate A , 8
44 stack_allocate B , 8
45 stack_allocate TMP , 8
46
47 mov %rsi , ARGV(%rbp)
48
49 ref8 ARGV , 1 , %rdi ; call atoi ; mov %rax , A(%rbp)
50 ref8 ARGV , 2 , %rdi ; call atoi ; mov %rax , B(%rbp)
51
52 mov A(%rbp) , %r10
53 mov B(%rbp) , %r11
54
55 cmp %r11 , %r10
56
57 jg 1f
58 jl 2f
59 je 3f
60
61 1:
62 print_literal_string "greater than\n"
63 jmp 4f
64 2:
65 print_literal_string "less than\n"
66 jmp 4f
67 3:
68 print_literal_string "equal\n"
69 jmp 4f
70 4:
71
72 return_integer $0
73 ```
74
75 # Assemble and run
76
77 $ gcc compare-ints.s && ./a.out 1 2
78 less than
79 $ gcc compare-ints.s && ./a.out 2 1
80 greater than
81 $ gcc compare-ints.s && ./a.out 1 1
82 equal
Something went wrong with that request. Please try again.