-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreadStr.asm
153 lines (118 loc) · 2.82 KB
/
readStr.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
section .data
; Define standard constants
LF equ 10 ; line feed
NULL equ 0 ; end of string
TRUE equ 1
FALSE equ 0
EXIT_SUCCESS equ 0 ; success code
STDIN equ 0 ; standard input
STDOUT equ 1 ; standard output
STDERR equ 2 ; standard error
SYS_read equ 0 ; read
SYS_write equ 1 ; write
SYS_open equ 2 ; file open
SYS_close equ 3 ; file close
SYS_fork equ 57 ; fork
SYS_exit equ 60 ; terminate
SYS_creat equ 85 ; file open/create
SYS_time equ 201 ; get time
STRLEN equ 51 ; The string length includes the terminating Null character
pmpt db "Enter Text: ", NULL ;
newLine db LF, NULL
section .bss
chr resb 1
str1 resb STRLEN+1
str2 resb STRLEN+1
section .text
global _start
_start:
mov rsi, STRLEN
mov rdi, str1
call readString
mov rsi, STRLEN
mov rdi, str2
call readString
exampleDone:
mov rax, SYS_exit
mov rdi, EXIT_SUCCESS
syscall
; FUNCTION INPUT:
; rsi contains the max length of the string
; rdi contains the string address
global readString
readString:
; Display prompt
push rdi
mov rdi, pmpt
call printString
pop rdi
; Read characters from user
mov rbx, rdi
mov r12, 0
mov r8, rsi ; store max length in r8
mov r9, rdi ; store string address in r9
readCharacters:
mov rax, SYS_read ; system code for read
mov rdi, STDIN ; standard in
lea rsi, byte[chr] ; address of chr (defined in the data section)
mov rdx, 1 ; count
syscall
mov al, byte[chr] ; get character
cmp al, LF ; compare input character to \n
je readDone ; finish reading if equal
inc r12 ; count++
cmp r12, r8 ; if #characters is more than max length, stop placing in the buffer
jae readCharacters
mov byte[rbx], al ; store character on the input address
inc rbx ; update address for the next character
jmp readCharacters
readDone:
mov byte[rbx], NULL ; NULL termination
; Output line
mov rdi, r9 ; print the string on the console
call printString
mov rax, r12 ; return the number of characters without NULL
ret
; print string function, the string should be null terminated
; arguments: address, string
global printString
printString:
push rbx
mov rbx, rdi
mov rdx, 0
strCountLoop:
cmp byte[rbx], NULL
je strCountDone
inc rdx
inc rbx
jmp strCountLoop
strCountDone:
cmp rdx, 0
je printDone
; Call OS to output string
mov rax, SYS_write
mov rsi, rdi
mov rdi, STDOUT
syscall
printDone:
pop rbx
ret
; find string length
; input is the address where the length is stored in rdi
; output is the length in rax
strLen:
push rbx
push rdx
mov rbx, rdi
mov rdx, 0
CountLoop:
cmp byte[rbx], NULL
je funcStrLenDone
inc rdx
inc rbx
jmp CountLoop
funcStrLenDone:
mov rax, rdx ; store the string length in rax
pop rdx
pop rbx
ret