-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathatbash.asm
More file actions
156 lines (126 loc) · 5.51 KB
/
atbash.asm
File metadata and controls
156 lines (126 loc) · 5.51 KB
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
154
155
156
; File: toupper.asm last updated 09/17/2018
;
; Convert user input to upper case.
;
; Assemble using NASM: nasm -f elf64 toupper.asm
; Link with ld: ld toupper.o -o toupper
;
%define STDIN 0
%define STDOUT 1
%define SYSCALL_EXIT 60
%define SYSCALL_READ 0
%define SYSCALL_WRITE 1
%define BUFLEN 256
SECTION .data ; initialized data section
msg1: db "Enter string: " ; user prompt
len1: equ $-msg1 ; length of first message
msg2: db "Original: " ; original string label
len2: equ $-msg2 ; length of second message
msg3: db "Converted: " ; converted string label
len3: equ $-msg3
msg4: db 10, "Read error", 10 ; error message
len4: equ $-msg4
SECTION .bss ; uninitialized data section
buf: resb BUFLEN ; buffer for read
newstr: resb BUFLEN ; converted string
count: resb 4 ; reserve storage for user input bytes
SECTION .text ; Code section.
global _start ; let loader see entry point
_start: nop ; Entry point.
start: ; address for gdb
; prompt user for input
;
mov rax, SYSCALL_WRITE ; write function
mov rdi, STDOUT ; Arg1: file descriptor
mov rsi, msg1 ; Arg2: addr of message
mov rdx, len1 ; Arg3: length of message
syscall ; 64-bit system call
; read user input
;
mov rax, SYSCALL_READ ; read function
mov rdi, STDIN ; Arg1: file descriptor
mov rsi, buf ; Arg2: addr of message
mov rdx, BUFLEN ; Arg3: length of message
syscall ; 64-bit system call
; error check
;
mov [count], rax ; save length of string read
cmp rax, 0 ; check if any chars read
jg read_OK ; >0 chars read = OK
mov rax, SYSCALL_WRITE ; Or Print Error Message
mov rdi, STDOUT ; Arg1: file descriptor
mov rsi, msg4 ; Arg2: addr of message
mov rdx, len4 ; Arg3: length of message
syscall ; 64-bit system call
jmp exit ; skip over rest
read_OK:
; Loop for upper case conversion
; assuming count > 0
;
L1_init:
mov rcx, [count] ; initialize count
mov rsi, buf ; point to start of buffer
mov rdi, newstr ; point to start of new string
L1_top:
mov al, [rsi] ; get a character
inc rsi ; update source pointer
cmp al, 'A' ; less than 'A'?
jb L1_cont ; skip to next character
cmp al, 'Z' ; less than 'Z'?
jbe L1_convd ;convert with upper case script
cmp al, 'a' ; less than 'a'
jb L1_cont ; skip to next character
cmp al, 'z' ;less than 'z'
jbe L1_convc ;convert with lower case script
jmp L1_cont ;jump to next character
L1_convc:
mov bh,'m' ;put 'm' into bh
sub bh,al ;find distance of 'm' to al in ascii
neg bh ;find negative of that distance
sub al,bh ;subtract value from al
sub al,bh ;subtract vaue again
add al,1 ;add one (necessary for algorithm)
jmp L1_cont ;jump to next character
L1_convd:
mov bh,'M' ;put 'M' into bh
sub bh,al ;find distance of 'M' to al in ascii
neg bh ;find negative of that distance
sub al,bh ;subtract value from al
sub al,bh ;subtract value agin
add al,1 ;add one
jmp L1_cont ;jump to next character
L1_cont:
mov [rdi], al ; store char in new string
inc rdi ; update dest pointer
dec rcx ; update char count
jnz L1_top ; loop to top if more chars
L1_end:
; print out user input for feedback
;
mov rax, SYSCALL_WRITE ; Print Message
mov rdi, STDOUT ; Arg1: file descriptor
mov rsi, msg2 ; Arg2: addr of message
mov rdx, len2 ; Arg3: length of message
syscall ; 64-bit system call
mov rax, SYSCALL_WRITE ; Write user input
mov rdi, STDOUT ; Arg1: file descriptor
mov rsi, buf ; Arg2: addr of message
mov rdx, [count] ; Arg3: length of message
syscall ; 64-bit system call
; print out converted string
;
mov rax, SYSCALL_WRITE ; Print Message
mov rdi, STDOUT ; Arg1: file descriptor
mov rsi, msg3 ; Arg2: addr of message
mov rdx, len3 ; Arg3: length of message
syscall ; 64-bit system call
mov rax, SYSCALL_WRITE ; Write out string
mov rdi, STDOUT ; Arg1: file descriptor
mov rsi, newstr ; Arg2: addr of message
mov rdx, [count] ; Arg3: length of message
syscall ; 64-bit system call
; final exit
;
exit: mov rax, SYSCALL_EXIT
mov rdi, 0 ; exit to shell
syscall