aboutsummaryrefslogtreecommitdiffstats
path: root/linux64/foo.asm
blob: 38dcca06e573a93b920aaad68526ed52f443872f (plain)
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
bits 64

section .data
	usage:		db 'usage: ', 0x00
	params:		db ' [string]', 0x00
	newline:	db 0x0a, 0x00

section .text
	global _start

_start:
	pop rax			; argc is on stack
	cmp rax,1 		; see if is 1 (no parameters)
	je help
	pop rsi			; program name
	pop rsi			; first parameter
	call print
	mov rsi, newline
	call print
	jmp exit

help:
	mov rsi, usage
	call print
	pop rsi
	call print
	mov rsi, params
	call print
	mov rsi, newline
	call print

	jmp exit

print:
	mov rdi, rsi		; copy, scasb uses edi
	xor al, al		; al = 0
	mov rcx, 4096		; maximum length 4096
				; this is used by REPNE
				; and not specific to SCASB
	cld
	repne scasb		; scan for '\0'
				; edi is now address of '\0'
	sub rdi, rsi		; rdi is now length of string
	mov rdx, rdi		; rdx : write(..., strlen)
	sub rdx, 1		; skip '\0'

	mov rax, 1		; syscall  write()
	mov rdi, 1		; rdi : write(fd, ...)
	syscall
	ret

exit:
	mov rax, 60		; exit()
	mov rdi, 0
	syscall