-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.lst
137 lines (137 loc) · 9.34 KB
/
test.lst
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
1 section .data
2 00000000 68656C6C6F20776F72- msg db "hello world"
3 00000009 6C64
4
5 section .text
6 global _start
7
8 _start:
9 00000000 B9[00000000] mov ecx,msg
10 00000005 89C8 mov eax,ecx
11 00000007 E87A000000 call sprintLF
12 0000000C E88B000000 call quit
13
14
15
16 ;------------------------------------------
17 ; void iprint(Integer number)
18 ; Integer printing function (itoa)
19 iprint:
20 00000011 50 push eax ; preserve eax on the stack to be restored after function runs
21 00000012 51 push ecx ; preserve ecx on the stack to be restored after function runs
22 00000013 52 push edx ; preserve edx on the stack to be restored after function runs
23 00000014 56 push esi ; preserve esi on the stack to be restored after function runs
24 00000015 B900000000 mov ecx, 0 ; counter of how many bytes we need to print in the end
25
26 divideLoop:
27 0000001A 41 inc ecx ; count each byte to print - number of characters
28 0000001B BA00000000 mov edx, 0 ; empty edx
29 00000020 BE0A000000 mov esi, 10 ; mov 10 into esi
30 00000025 F7FE idiv esi ; divide eax by esi
31 00000027 83C230 add edx, 48 ; convert edx to it's ascii representation - edx holds the remainder after a divide instruction
32 0000002A 52 push edx ; push edx (string representation of an intger) onto the stack
33 0000002B 83F800 cmp eax, 0 ; can the integer be divided anymore?
34 0000002E 75EA jnz divideLoop ; jump if not zero to the label divideLoop
35
36 printLoop:
37 00000030 49 dec ecx ; count down each byte that we put on the stack
38 00000031 89E0 mov eax, esp ; mov the stack pointer into eax for printing
39 00000033 E830000000 call sprint ; call our string print function
40 00000038 58 pop eax ; remove last character from the stack to move esp forward
41 00000039 83F900 cmp ecx, 0 ; have we printed all bytes we pushed onto the stack?
42 0000003C 75F2 jnz printLoop ; jump is not zero to the label printLoop
43
44 0000003E 5E pop esi ; restore esi from the value we pushed onto the stack at the start
45 0000003F 5A pop edx ; restore edx from the value we pushed onto the stack at the start
46 00000040 59 pop ecx ; restore ecx from the value we pushed onto the stack at the start
47 00000041 58 pop eax ; restore eax from the value we pushed onto the stack at the start
48 00000042 C3 ret
49
50
51 ;------------------------------------------
52 ; void iprintLF(Integer number)
53 ; Integer printing function with linefeed (itoa)
54 iprintLF:
55 00000043 E8C9FFFFFF call iprint ; call our integer printing function
56
57 00000048 50 push eax ; push eax onto the stack to preserve it while we use the eax register in this function
58 00000049 B80A000000 mov eax, 0Ah ; move 0Ah into eax - 0Ah is the ascii character for a linefeed
59 0000004E 50 push eax ; push the linefeed onto the stack so we can get the address
60 0000004F 89E0 mov eax, esp ; move the address of the current stack pointer into eax for sprint
61 00000051 E812000000 call sprint ; call our sprint function
62 00000056 58 pop eax ; remove our linefeed character from the stack
63 00000057 58 pop eax ; restore the original value of eax before our function was called
64 00000058 C3 ret
65
66
67 ;------------------------------------------
68 ; int slen(String message)
69 ; String length calculation function
70 slen:
71 00000059 53 push ebx
72 0000005A 89C3 mov ebx, eax
73
74 nextchar:
75 0000005C 803800 cmp byte [eax], 0
76 0000005F 7403 jz finished
77 00000061 40 inc eax
78 00000062 EBF8 jmp nextchar
79
80 finished:
81 00000064 29D8 sub eax, ebx
82 00000066 5B pop ebx
83 00000067 C3 ret
84
85
86 ;------------------------------------------
87 ; void sprint(String message)
88 ; String printing function
89 sprint:
90 00000068 52 push edx
91 00000069 51 push ecx
92 0000006A 53 push ebx
93 0000006B 50 push eax
94 0000006C E8E8FFFFFF call slen
95
96 00000071 89C2 mov edx, eax
97 00000073 58 pop eax
98
99 00000074 89C1 mov ecx, eax
100 00000076 BB01000000 mov ebx, 1
101 0000007B B804000000 mov eax, 4
102 00000080 CD80 int 80h
103
104 00000082 5B pop ebx
105 00000083 59 pop ecx
106 00000084 5A pop edx
107 00000085 C3 ret
108
109
110 ;------------------------------------------
111 ; void sprintLF(String message)
112 ; String printing with line feed function
113 sprintLF:
114 00000086 E8DDFFFFFF call sprint
115
116 0000008B 50 push eax
117 0000008C B80A000000 mov eax, 0AH
118 00000091 50 push eax
119 00000092 89E0 mov eax, esp
120 00000094 E8CFFFFFFF call sprint
121 00000099 58 pop eax
122 0000009A 58 pop eax
123 0000009B C3 ret
124
125
126 ;------------------------------------------
127 ; void exit()
128 ; Exit program and restore resources
129 quit:
130 0000009C BB00000000 mov ebx, 0
131 000000A1 B801000000 mov eax, 1
132 000000A6 CD80 int 80h
133 000000A8 C3 ret
134
135
136
137