This is a homework if anyone’s curious.
The task is to have a C program create a 2D array and pass that array into assembly which it should be sorted using selection sort.
The error I’m having when trying to run it is segmentation fault. I’ve tried putting
push tesst
push sout
call printf
to print out “test” on the terminal window but I couldn’t figure out where it’s coming from. I also tried putting it right after “enter 0,0” and I still havent gotten a print out of test. So i’m completely out of ideas as to where i’m getting the segmentation fault.
Any assistance would be very much appreciated. 🙁 Due tomorrow night and i’ve been stuck at this for a couple of days. Someone shed some light please
-Chris
The codes will be below
my c program
#include <stdio.h> int ssort(char * hi[], int x, int y); int main(){ int i, j; char hi[3][5] = {"Yoshi", "Annie", "Chris"}; printf("Display Unsorted Strings\n"); for(i = 0; i < 3; i++){ for(j = 0; j < 5; j++){ printf("%c", hi[i][j]); } printf("\n"); } printf("It Got Here\n"); ssort((char*)hi, 5, 3); printf("Sorted\n"); for(i = 0; i < 3; i++){ for(j = 0; j < 5; j++){ printf("%c", hi[i][j]); } printf("\n"); } }
And my Assembly Code
extern printf segment .data tesst db 't','e','s','t',0 sout db "%s", 10, 0 segment .text global ssort ssort: enter 0,0 ;;; for(i = 0; i < namecount; i++){ ;;; for(j = 1; j < namecount; j++){ ;;; if(array[i] < array[j]) ;;; do nothing ;;; else ;;; swap ;;; } ;;; } mov esi, [ebp+8] mov edi, esi add edi, [ebp+8] mov ecx, 0 ;i mov edx, 0 add edx, 1 ;j ;; [ebp+16] = namecount iloop: push esi cmp ecx, [ebp+16] je done_sorting jloop: cmp edx, [ebp+16] je j_done ;;; compare here now compare: mov al, [esi] mov bl, [edi] cmp al, bl jg alisbigger jl blisbigger inc esi inc edi jmp compare alisbigger: jmp swap blisbigger: jmp done swap: mov ebx, 0 mov ebx, [ebp+12] swap_loop: dec ebx mov al, [esi] mov bl, [edi] mov [esi], bl mov [edi], al inc esi inc edi cmp ebx, 0 je done jmp swap_loop done: inc edx jmp jloop j_done: add ecx, 1 add esi, [ebp+12] mov edx, 1 jmp iloop done_sorting: leave ret
Print-style debugging is okay in situations where there’s nothing else available but nothing beats getting down and dirty inside a source level debugger.
Step 1: Probably the easiest way is to first take out the call into assembly and run it, to ensure it’s not your C code causing the problem.
Step 2: Then, put the call back in and load it into
gdbor your favorite source level debugger. Set a breakpoint on the first assembler instruction. When it breaks, examine the stack to ensure it’s what you expect.Step 3: Then single step through the assembly until you find your problem.
That will be the best way for you to learn, and to quickly find and fix your problem.
I will tell you one thing I see as a potential problem. Just after the
ilooplabel, you havepush esi. I can’t see apop(or otheresp-modifying statement) anywhere in your code and, worse yet, thatpushis happening in a loop.If you try to return from a function with the stack pointer different to what it was when you entered, you’re in for a world of pain. I may just have missed something but I’d start by looking there.