-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.asm
139 lines (102 loc) · 3.84 KB
/
main.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
%include 'functions.asm'
SECTION .data
shiftMsg db "Enter amount to shift by: "
lenshiftMsg equ $ - shiftMsg
sentenceMsg db "Enter sentence to cypher: "
lensentenceMsg equ $ - sentenceMsg
resultMsg db `Result: \u001b[32m` ; Result will be printed in green
lenResultMsg equ $ - resultMsg
SECTION .bss ; Section containing uninitialized data
numStr resb 11 ; 11 byte for shiftNum string
num resb 4 ; 4 byte for shiftNum integer
sentence resb 4096 ; 4096 byte for sentence
SECTION .text ; Section containing code
global _start
_start:
nop
; Print: 'Enter amount to shift by: '
mov eax, shiftMsg ; Message to print
mov ebx, lenshiftMsg ; Buffer size
call print
; Read number
mov eax, 3 ; sys_read
mov ebx, 0 ; stdin
mov ecx, numStr ; Store input here
mov edx, 11 ; Read 11 byte
int 0x80 ; syscall
; Print: 'Enter sentence to cypher: '
mov eax, sentenceMsg ; Message to print
mov ebx, lensentenceMsg ; Buffer size
call print
; Read sentence
mov eax, 3 ; sys_read
mov ebx, 0 ; stdin
mov ecx, sentence ; Store input here
mov edx, 4096 ; Buffer size
int 0x80 ; syscall
; Get sentence length
mov ebx, sentence ; Prepare EBX for slen function
call slen ; Call slen, which counts string length
push eax ; Save length
mov ecx, eax ; Put lenght in ecx
dec ecx ; Decrement by 1 (so we don't shift \n)
; Convert input number (which is string) to integer
mov ebx, numStr
call to_int
; shiftNum = shiftNum % 26
mov ebx, 26
call remainder ; eax = eax%26
mov ebx, eax ; EBX = number to shift by
mov eax, sentence ; EAX = sentence to shift
do_more:
call shift_letter
inc eax ; Move onto next character
dec ecx ; Update counter
jnz do_more ; Jump while counter != 0
; Now sentence is shifted, so we print it out
; Print: 'Result: '
mov eax, resultMsg
mov ebx, lenResultMsg
call print
; Print cyphered sentence
mov eax, sentence ; Message to print
pop ebx ; Message length
call print ; Print message
; Exit
mov eax, 1
int 0x80
;---------------------------------------------------------------
; void shift_letter -- Shifts a character by given amount
;
; IN:
; EAX: Character to shift
; EBX: Number to shift by
; OUT:
; None
;---------------------------------------------------------------
shift_letter:
; Check if current char is a letter
; If its letter, check upper or lower case
; and call corresponding function
cmp byte[eax], 'A' ; Is char lower than 'A'
jb .return ; If yes, return
cmp byte[eax], 'Z' ; Is lower or equal to 'Z'
jbe .shift_upper ; If yes, we have upper case letter
cmp byte[eax], 'z' ; Is above 'z'
ja .return ; If yes, return
cmp byte[eax], 'a' ; Is above 'a'
jae .shift_lower ; If yes, we have lower case letter
jmp .return
.shift_upper:
add byte [eax], bl ; Shift the letter
cmp byte [eax], 'Z' ; Is it below or equal to 'Z'?
jbe .return ; If yes, finish functin
sub byte [eax], 26 ; If not, substract 26
jmp .return
.shift_lower:
add byte [eax], bl ; Shift the letter
cmp byte [eax], 'z' ; Is it below or equal to 'z'?
jbe .return ; If yes, finish functin
sub byte [eax], 26 ; If not, substract 26
.return:
ret