Skip to content

Commit 5b390f1

Browse files
authored
chapter 13, problem 2
1 parent c7ed514 commit 5b390f1

1 file changed

Lines changed: 153 additions & 0 deletions

File tree

readStr.asm

Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
section .data
2+
3+
; Define standard constants
4+
5+
LF equ 10 ; line feed
6+
NULL equ 0 ; end of string
7+
TRUE equ 1
8+
FALSE equ 0
9+
10+
EXIT_SUCCESS equ 0 ; success code
11+
12+
STDIN equ 0 ; standard input
13+
STDOUT equ 1 ; standard output
14+
STDERR equ 2 ; standard error
15+
16+
SYS_read equ 0 ; read
17+
SYS_write equ 1 ; write
18+
SYS_open equ 2 ; file open
19+
SYS_close equ 3 ; file close
20+
SYS_fork equ 57 ; fork
21+
SYS_exit equ 60 ; terminate
22+
SYS_creat equ 85 ; file open/create
23+
SYS_time equ 201 ; get time
24+
25+
STRLEN equ 51 ; The string length includes the terminating Null character
26+
27+
pmpt db "Enter Text: ", NULL ;
28+
newLine db LF, NULL
29+
30+
section .bss
31+
chr resb 1
32+
str1 resb STRLEN+1
33+
str2 resb STRLEN+1
34+
35+
section .text
36+
37+
global _start
38+
_start:
39+
40+
mov rsi, STRLEN
41+
mov rdi, str1
42+
call readString
43+
44+
mov rsi, STRLEN
45+
mov rdi, str2
46+
call readString
47+
48+
exampleDone:
49+
mov rax, SYS_exit
50+
mov rdi, EXIT_SUCCESS
51+
syscall
52+
53+
; FUNCTION INPUT:
54+
; rsi contains the max length of the string
55+
; rdi contains the string address
56+
57+
global readString
58+
readString:
59+
; Display prompt
60+
push rdi
61+
mov rdi, pmpt
62+
call printString
63+
pop rdi
64+
65+
; Read characters from user
66+
67+
mov rbx, rdi
68+
mov r12, 0
69+
mov r8, rsi ; store max length in r8
70+
mov r9, rdi ; store string address in r9
71+
72+
readCharacters:
73+
mov rax, SYS_read ; system code for read
74+
mov rdi, STDIN ; standard in
75+
lea rsi, byte[chr] ; address of chr (defined in the data section)
76+
mov rdx, 1 ; count
77+
syscall
78+
79+
mov al, byte[chr] ; get character
80+
cmp al, LF ; compare input character to \n
81+
je readDone ; finish reading if equal
82+
83+
inc r12 ; count++
84+
cmp r12, r8 ; if #characters is more than max length, stop placing in the buffer
85+
jae readCharacters
86+
87+
mov byte[rbx], al ; store character on the input address
88+
inc rbx ; update address for the next character
89+
90+
jmp readCharacters
91+
92+
readDone:
93+
mov byte[rbx], NULL ; NULL termination
94+
; Output line
95+
96+
mov rdi, r9 ; print the string on the console
97+
call printString
98+
mov rax, r12 ; return the number of characters without NULL
99+
100+
ret
101+
102+
; print string function, the string should be null terminated
103+
; arguments: address, string
104+
105+
global printString
106+
printString:
107+
push rbx
108+
mov rbx, rdi
109+
mov rdx, 0
110+
111+
strCountLoop:
112+
cmp byte[rbx], NULL
113+
je strCountDone
114+
inc rdx
115+
inc rbx
116+
jmp strCountLoop
117+
118+
strCountDone:
119+
cmp rdx, 0
120+
je printDone
121+
; Call OS to output string
122+
mov rax, SYS_write
123+
mov rsi, rdi
124+
mov rdi, STDOUT
125+
126+
syscall
127+
128+
printDone:
129+
pop rbx
130+
ret
131+
132+
; find string length
133+
; input is the address where the length is stored in rdi
134+
; output is the length in rax
135+
136+
strLen:
137+
push rbx
138+
push rdx
139+
mov rbx, rdi
140+
mov rdx, 0
141+
142+
CountLoop:
143+
cmp byte[rbx], NULL
144+
je funcStrLenDone
145+
inc rdx
146+
inc rbx
147+
jmp CountLoop
148+
149+
funcStrLenDone:
150+
mov rax, rdx ; store the string length in rax
151+
pop rdx
152+
pop rbx
153+
ret

0 commit comments

Comments
 (0)