Debugging Segmentation Fault in Loop for String Comparison and Compression in x86 Assembly

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<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.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<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.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật