-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest.asm
155 lines (118 loc) · 3.3 KB
/
test.asm
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
; print square of zeros and ones
%ifidn __OUTPUT_FORMAT__, elf32
%elifidn __OUTPUT_FORMAT__, elf64
%else
%fatal -f elf32 or elf64 required!
%endif
section .data
uinput: db 0
buf_sz: db 255
zerone: dw 0x3031
onezer: dw 0x3130
mem_at: dd 0
mem_sz: dd 0
inputmsg: db 'input> '
inputlen: equ $ - inputmsg
errormsg: db 'error.', 0x0a
errorlen: equ $ - errormsg
section .bss
buffer: resb 255
section .text
global _start
_start:
mov eax, 4 ; sys_write
mov ebx, 1 ; stdout
mov ecx, inputmsg
mov edx, inputlen
int 80h
mov eax, 3 ; sys_read
mov ebx, 2 ; stdin
mov ecx, buffer
mov edx, buf_sz
int 80h
test eax, eax
js .error ; jump on error (-1)
mov ecx, 0 ; ecx is next used as
; exit code or counter
sub eax, 1
test eax, eax
jz .quit ; no input given :-(
mov esi, buffer
.nextint:
mov dl, byte [esi] ; take a byte
inc esi ; (*buffer)++
sub dl, 48
imul ecx, 10
add cl, dl
sub eax, 1 ; if there's
test eax, eax ; more to read,
jnz .nextint ; read more
mov [uinput], cl ; save user input
mov eax, ecx ; calc required memory
inc eax ; sidenote: nice thing is that n(n+1)
mul ecx ; is always an even number
mov ecx, eax
mov [mem_sz], eax ; save for later
.bork:
mov eax, 45
mov ebx, 0
int 80h ; get original break..
mov edi, eax ; ..and keep it
add eax, ecx
mov ebx, eax
mov eax, 45
int 80h ; new break at eax if success
test edi, eax ; old and new break
je .quit ; should NOT be equal (brk(2))
mov [mem_at], edi ; save mem ptr
mov ax, word [zerone] ; lets initialize memory we have
shr ecx, 1
rep stosw ; now it should read 010101...
mov bl, [uinput]
and ebx, 0x01
test ebx, ebx
jz .even
mov bl, [uinput]
shr bl, 1
mov ax, [onezer]
mov edi, [mem_at]
.odds:
mov cl, [uinput]
add edi, ecx
inc edi
shr ecx, 1
inc ecx
rep stosw
dec ebx
test ebx, ebx
jnz .odds
.even:
mov eax, 0x0a ; newlines for the masses
mov edi, [mem_at]
mov cl, [uinput]
mov edx, ecx
inc edx
.newline:
add edi, edx
mov [edi], al
loop .newline
.prnt:
mov eax, 4
mov ebx, 1
mov ecx, [mem_at]
mov edx, [mem_sz]
inc ecx
int 80h
.quit:
mov ebx, ecx ; exit(ecx)
mov eax, 1
int 80h
.error:
mov eax, 4 ; print err msg
mov ebx, 1
mov ecx, errormsg
mov edx, errorlen
int 80h
mov eax, 1 ; sys_exit
mov ebx, 1 ; exit code 1
int 80h