<code>#include <string>
#include <vector>
using namespace std;
int Min(int A, int B) {
return A < B ? A : B;
}
int solution(string s) {
int answer = s.length();
for (int Len = 1; Len <= s.length() / 2; Len++) {
string Result = "";
string Temp = s.substr(0, Len);
int Cnt = 1;
int i;
for (i = Len; i <= s.length(); i = i + Len) {
if (Temp == s.substr(i, Len))
Cnt++;
else {
if (Cnt == 1)
Result += Temp;
else {
Result += to_string(Cnt);
Result += Temp;
}
Temp = s.substr(i, Len);
Cnt = 1;
}
}
if (i > s.length()) {
if (Cnt == 1)
Result += Temp;
else {
Result += to_string(Cnt);
Result += Temp;
}
}
answer = Min(answer, Result.length());
}
return answer;
}
</code>
<code>#include <string>
#include <vector>
using namespace std;
int Min(int A, int B) {
return A < B ? A : B;
}
int solution(string s) {
int answer = s.length();
for (int Len = 1; Len <= s.length() / 2; Len++) {
string Result = "";
string Temp = s.substr(0, Len);
int Cnt = 1;
int i;
for (i = Len; i <= s.length(); i = i + Len) {
if (Temp == s.substr(i, Len))
Cnt++;
else {
if (Cnt == 1)
Result += Temp;
else {
Result += to_string(Cnt);
Result += Temp;
}
Temp = s.substr(i, Len);
Cnt = 1;
}
}
if (i > s.length()) {
if (Cnt == 1)
Result += Temp;
else {
Result += to_string(Cnt);
Result += Temp;
}
}
answer = Min(answer, Result.length());
}
return answer;
}
</code>
#include <string>
#include <vector>
using namespace std;
int Min(int A, int B) {
return A < B ? A : B;
}
int solution(string s) {
int answer = s.length();
for (int Len = 1; Len <= s.length() / 2; Len++) {
string Result = "";
string Temp = s.substr(0, Len);
int Cnt = 1;
int i;
for (i = Len; i <= s.length(); i = i + Len) {
if (Temp == s.substr(i, Len))
Cnt++;
else {
if (Cnt == 1)
Result += Temp;
else {
Result += to_string(Cnt);
Result += Temp;
}
Temp = s.substr(i, Len);
Cnt = 1;
}
}
if (i > s.length()) {
if (Cnt == 1)
Result += Temp;
else {
Result += to_string(Cnt);
Result += Temp;
}
}
answer = Min(answer, Result.length());
}
return answer;
}
I’m working on writing the x86 version of the above code, which includes a loop for comparing and compressing two strings. However, my code is causing a segmentation fault, and I suspect that the following loop is causing the issue. This is my x86 code.
<code>compress:
mov rcx, len
cmp rcx, 1
je len1
shr rcx, 1 ; Repeat for half the length of the string, rcx = len/2
mov rbx, 1 ; rbx = i, compression unit for the string
mov rdx, len ; rdx stores the length of the array
compress_loop:
cmp rbx,rcx ; If i > len/2, exit loop
jg done
push rbx
push rcx
push rdx
lea rsi, [array]
lea rdi, [temp]
lea r8, [result]
call substr ; Copy i elements of array to temp
xor r10, r10
inc r10 ; count = 1
mov r11, rbx ; r11 = j
call compare
push rsi
call find_len
pop rsi
push rax
call clean
pop rax
pop rdx
pop rcx
pop rbx
inc rbx ; i++
cmp rdx, rax ; Compare rax with the length of the array
ja find_answer ; Store the smaller value in rdx
jmp compress_loop
compare:
cmp r11, rdx ; If j > len, exit loop
jg compare_done
mov r12, rsi
add r12, r11 ; r12 = array[j]
mov r13, rdi ; r13 = temp
jmp compare_loop1
compare_loop1:
mov al, byte[r13]
mov bl, byte[r12]
cmp al, 0 ; Until temp is finished, if al = bl, count + 1
je count_plus
cmp al, bl
je compare_loop2
jmp new
compare_loop2:
inc r13
inc r12
jmp compare_loop1
compare_done :
ret
count_plus:
inc r10 ; count + 1
add r11, rbx ; j = j + i
jmp compare
new :
cmp r10, 1
je copy_count1
jmp copy_string
copy_count1:
; If count = 1, store temp in result, and store a new string in temp
push rcx
push rsi ; array
push rdi ; temp
mov rsi, rdi
mov rdi, r8
mov rcx, rbx
rep movsb ; Store temp in result
pop rdi ; rdi = temp
call clean_temp ; Initialize temp to 0
pop rsi
add rsi, r11 ; rsi = array[j]
rep movsb ; Store a new string in temp
pop rcx
sub rdi, rbx ; rdi = temp[0]
lea rsi, [array] ; rsi = array[0]
mov r10, 1 ; count = 1
add r11, rbx ; j = j + i
jmp compare
copy_string :
; If count != 1, store temp + count in result
push rcx
push rsi
push rdi
push rax
mov rsi, rdi
mov rdi, r8
mov rcx, rbx
rep movsb ; Store temp in result
mov rax, r10
call copy_count ; Add count to result
pop rax
pop rdi
call clean_temp
pop rsi
add rsi, r11 ; rsi = array[j]
rep movsb ; Store a new string in temp
pop rcx
sub rdi, rbx ; rdi = temp[0]
lea rsi, [array] ; rsi = array[0]
mov r10, 1 ; count = 1
add r11, rbx ; j = j + i
jmp compare
</code>
<code>compress:
mov rcx, len
cmp rcx, 1
je len1
shr rcx, 1 ; Repeat for half the length of the string, rcx = len/2
mov rbx, 1 ; rbx = i, compression unit for the string
mov rdx, len ; rdx stores the length of the array
compress_loop:
cmp rbx,rcx ; If i > len/2, exit loop
jg done
push rbx
push rcx
push rdx
lea rsi, [array]
lea rdi, [temp]
lea r8, [result]
call substr ; Copy i elements of array to temp
xor r10, r10
inc r10 ; count = 1
mov r11, rbx ; r11 = j
call compare
push rsi
call find_len
pop rsi
push rax
call clean
pop rax
pop rdx
pop rcx
pop rbx
inc rbx ; i++
cmp rdx, rax ; Compare rax with the length of the array
ja find_answer ; Store the smaller value in rdx
jmp compress_loop
compare:
cmp r11, rdx ; If j > len, exit loop
jg compare_done
mov r12, rsi
add r12, r11 ; r12 = array[j]
mov r13, rdi ; r13 = temp
jmp compare_loop1
compare_loop1:
mov al, byte[r13]
mov bl, byte[r12]
cmp al, 0 ; Until temp is finished, if al = bl, count + 1
je count_plus
cmp al, bl
je compare_loop2
jmp new
compare_loop2:
inc r13
inc r12
jmp compare_loop1
compare_done :
ret
count_plus:
inc r10 ; count + 1
add r11, rbx ; j = j + i
jmp compare
new :
cmp r10, 1
je copy_count1
jmp copy_string
copy_count1:
; If count = 1, store temp in result, and store a new string in temp
push rcx
push rsi ; array
push rdi ; temp
mov rsi, rdi
mov rdi, r8
mov rcx, rbx
rep movsb ; Store temp in result
pop rdi ; rdi = temp
call clean_temp ; Initialize temp to 0
pop rsi
add rsi, r11 ; rsi = array[j]
rep movsb ; Store a new string in temp
pop rcx
sub rdi, rbx ; rdi = temp[0]
lea rsi, [array] ; rsi = array[0]
mov r10, 1 ; count = 1
add r11, rbx ; j = j + i
jmp compare
copy_string :
; If count != 1, store temp + count in result
push rcx
push rsi
push rdi
push rax
mov rsi, rdi
mov rdi, r8
mov rcx, rbx
rep movsb ; Store temp in result
mov rax, r10
call copy_count ; Add count to result
pop rax
pop rdi
call clean_temp
pop rsi
add rsi, r11 ; rsi = array[j]
rep movsb ; Store a new string in temp
pop rcx
sub rdi, rbx ; rdi = temp[0]
lea rsi, [array] ; rsi = array[0]
mov r10, 1 ; count = 1
add r11, rbx ; j = j + i
jmp compare
</code>
compress:
mov rcx, len
cmp rcx, 1
je len1
shr rcx, 1 ; Repeat for half the length of the string, rcx = len/2
mov rbx, 1 ; rbx = i, compression unit for the string
mov rdx, len ; rdx stores the length of the array
compress_loop:
cmp rbx,rcx ; If i > len/2, exit loop
jg done
push rbx
push rcx
push rdx
lea rsi, [array]
lea rdi, [temp]
lea r8, [result]
call substr ; Copy i elements of array to temp
xor r10, r10
inc r10 ; count = 1
mov r11, rbx ; r11 = j
call compare
push rsi
call find_len
pop rsi
push rax
call clean
pop rax
pop rdx
pop rcx
pop rbx
inc rbx ; i++
cmp rdx, rax ; Compare rax with the length of the array
ja find_answer ; Store the smaller value in rdx
jmp compress_loop
compare:
cmp r11, rdx ; If j > len, exit loop
jg compare_done
mov r12, rsi
add r12, r11 ; r12 = array[j]
mov r13, rdi ; r13 = temp
jmp compare_loop1
compare_loop1:
mov al, byte[r13]
mov bl, byte[r12]
cmp al, 0 ; Until temp is finished, if al = bl, count + 1
je count_plus
cmp al, bl
je compare_loop2
jmp new
compare_loop2:
inc r13
inc r12
jmp compare_loop1
compare_done :
ret
count_plus:
inc r10 ; count + 1
add r11, rbx ; j = j + i
jmp compare
new :
cmp r10, 1
je copy_count1
jmp copy_string
copy_count1:
; If count = 1, store temp in result, and store a new string in temp
push rcx
push rsi ; array
push rdi ; temp
mov rsi, rdi
mov rdi, r8
mov rcx, rbx
rep movsb ; Store temp in result
pop rdi ; rdi = temp
call clean_temp ; Initialize temp to 0
pop rsi
add rsi, r11 ; rsi = array[j]
rep movsb ; Store a new string in temp
pop rcx
sub rdi, rbx ; rdi = temp[0]
lea rsi, [array] ; rsi = array[0]
mov r10, 1 ; count = 1
add r11, rbx ; j = j + i
jmp compare
copy_string :
; If count != 1, store temp + count in result
push rcx
push rsi
push rdi
push rax
mov rsi, rdi
mov rdi, r8
mov rcx, rbx
rep movsb ; Store temp in result
mov rax, r10
call copy_count ; Add count to result
pop rax
pop rdi
call clean_temp
pop rsi
add rsi, r11 ; rsi = array[j]
rep movsb ; Store a new string in temp
pop rcx
sub rdi, rbx ; rdi = temp[0]
lea rsi, [array] ; rsi = array[0]
mov r10, 1 ; count = 1
add r11, rbx ; j = j + i
jmp compare
I’m not sure how to debug this code. Help me find where the problem is.