-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlab1.asm
executable file
·97 lines (77 loc) · 2.26 KB
/
lab1.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
section .data
yn db "Your name: "
lenYn equ $-yn
hl db "Hello "
lenHl equ $-hl
excl db " !!", 0x0A ; from line 46 will strip all new line byte (0x0A) so we add it after '!!'
lenExcl equ $-excl
wlc db "Welcome, "
lenWlc equ $-wlc
ehc db " to EHC"
lenEhc equ $-ehc
section .bss
name resb 50
section .text
global _start
_start:
mov eax, 4 ; sys write
mov ebx, 1 ; stdout
mov ecx, yn ; your name
mov edx, lenYn ; your name length
int 80h ; sys call
; eax = 4 corresponds to the sys_write system call for writing to a file descriptor.
mov eax, 3 ; sys read
mov ebx, 0 ; from stdin
mov ecx, name ; name
mov edx, 50 ; name length
int 80h ; sys call
; eax = 3 corresponds to the sys_read system call for reading from a file descriptor.
; The esi register is often used for string operations,
;memory addressing, and as a general-purpose register
;for holding memory addresses or indices during various computations
mov esi, name ; point to the start of the buffer
; moves the memory address of the name buffer into the esi register
add esi, edx ; move esi to the end of the buffer by add length of name bytes to esi
find_newline:
; Decrements the esi register
dec esi ; move back one byte
cmp byte [esi], 0x0A ; check if the byte is newline character '\n'
je replace_newline ; if it is, jump to replace_newline
cmp esi, name ; check if we've reached the start of the buffer
jg find_newline ; if not, continue searching
replace_newline:
mov byte [esi], 0x00 ; replace newline with null terminator '\0'
mov eax, 4
mov ebx, 1
mov ecx, hl
mov edx, lenHl
int 80h
mov eax, 4
mov ebx, 1
mov ecx, name
mov edx, 50
int 80h
mov eax, 4
mov ebx, 1
mov ecx, excl
mov edx, lenExcl
int 80h
mov eax, 4
mov ebx, 1
mov ecx, wlc
mov edx, lenWlc
int 80h
mov eax, 4
mov ebx, 1
mov ecx, name
mov edx, 10
int 80h
mov eax, 4
mov ebx, 1
mov ecx, ehc
mov edx, lenEhc
int 80h
mov eax, 1
int 80h
ret
; usage with b.sh: `./b.sh lab1.asm; ./lab1/lab1`