Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7875099
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T02:55:35+00:00 2026-06-03T02:55:35+00:00

My assembler is YASM and I am coding on 64-bit Linux. I assemble using

  • 0

My assembler is YASM and I am coding on 64-bit Linux.

I assemble using yasm -f elf -m amd64 -g dwarf2 filename.asm and link using ld

I’m trying to implement selection sort. rdi and rsi are pointing to various parts of a strbuf2 resb 10 array. What could possibly be the reason for this segmentation fault? Lines 105 and 106 do the exact same type of operation, so why does it crash on line 106 but not line 105?

I’ve included the relevant portion of the code, and the gdbtui screenshot when it crashes.

UPDATE: Counters fixed

; ====== Sorting begins here ======
; Register uses:
; bpl holds the current minimum value
; r8 holds the memory address of the current minimum value
; rdi points to the boundary of the "outer loop"
; rsi points to the boundary of the "inner loop"
sorting:
    mov rdi, strbuf2  ; outer loop pointer
    mov rsi, strbuf2+1  ; inner loop pointer
    mov rax, 1  ; inner loop counter
    mov rbx, 0  ; outer loop counter

innerloop:
    mov bpl, [rdi] ; assume beginning element of unsorted array is minimum

    ; store the value of first element of unsorted array
    mov dl, [rdi]

    ; compare the current small value with the value in rsi
    mov cl, [rsi]   
    cmp bpl, cl 
    jg  new_small

    inc rsi
    inc rax
    cmp rax, 9
    jle innerloop
    jg  innerloop_done

new_small:
    inc rax
    mov bpl, cl; save the new small value
    mov r8, rsi  ; save its index 
    inc rsi 
    cmp rax, 9
    jle     innerloop

innerloop_done:
    ; When the inner loop is completed...
    ; First, do the swap
    ; to swap r8 (target memory address)  with [rdi] (outer array boundary)
    mov dl, 0 ; initialise
    mov dl, [rdi]
    mov [rdi], bpl
    mov [r8], dl 

    inc rdi  ; move the outer loop pointer forward
    inc rsi  ; move the inner loop pointer forward
    inc rbx  ; increment the outer loop counter (the unsorted array becomes smaller)

    ; set the inner loop counter to the appropriate position
    mov rax, 1 
    add rax, rbx  ; now rax (inner loop counter)
                  ; will always be rbx+1 (outer loop counter + 1) 
    cmp rbx, 9  
    jle innerloop
; ====== Sorting ends here ======

Segmentation fault gdb output

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T02:55:36+00:00Added an answer on June 3, 2026 at 2:55 am

    I think you’re getting lost in the details of the implementation and forgetting what the code should do. I suggest that you first implement the code in C and then gradually change it to become ASM-like until the point when you can write it in ASM fully.

    Note the progression from the small, clean and easy to understand implementation in C in sortC1() to the somewhat messy but completely equivalent ASM-like implementation in sortAsm(). Use your favorite file comparison tool to see what changes between the implementations.

    The code:

    #include <stdio.h>
    #include <string.h>
    
    char strOriginal[11] = "8163045297";
    char str[11];
    
    void sortC1(void)
    {
      int outIdx, inIdx, minIdx;
      char min, tmp;
    
      for (outIdx = 0; outIdx < 10; outIdx++)
      {
        minIdx = outIdx;
        min = str[minIdx];
    
        for (inIdx = outIdx; inIdx < 10; inIdx++)
        {
          if (min > str[inIdx])
          {
            minIdx = inIdx;
            min = str[minIdx];
          }
        }
    
        tmp = str[outIdx];
        str[outIdx] = min;
        str[minIdx] = tmp;
      }
    }
    
    void sortC2(void)
    {
      char *outPtr, *inPtr, *minPtr;
      int outCnt, inCnt;
      char min, tmp;
    
      for (outPtr = str, outCnt = 0;
           outCnt < 10;
           outPtr++, outCnt++)
      {
        minPtr = outPtr;
        min = *minPtr;
    
        for (inPtr = outPtr, inCnt = 10 - outCnt;
             inCnt > 0;
             inPtr++, inCnt--)
        {
          if (min > *inPtr)
          {
            minPtr = inPtr;
            min = *minPtr;
          }
        }
    
        tmp = *outPtr;
        *outPtr = min;
        *minPtr = tmp;
      }
    }
    
    void sortC3(void)
    {
      char *outPtr, *inPtr, *minPtr;
      int outCnt, inCnt;
      char min, tmp;
    
      outPtr = str;
      outCnt = 0;
    
      while (outCnt < 10)
      {
        minPtr = outPtr;
        min = *minPtr;
    
        inPtr = outPtr;
        inCnt = 10 - outCnt;
    
        while (inCnt > 0)
        {
          if (min > *inPtr)
          {
            minPtr = inPtr;
            min = *minPtr;
          }
    
          inPtr++;
          inCnt--;
        }
    
        tmp = *outPtr;
        *outPtr = min;
        *minPtr = tmp;
    
        outPtr++;
        outCnt++;
      }
    }
    
    void sortC4(void)
    {
      char *outPtr, *inPtr, *minPtr;
      int outCnt, inCnt;
      char min, tmp;
    
      outPtr = str;
      outCnt = 0;
    
    outerloop:
    
      minPtr = outPtr;
      min = *minPtr;
    
      inPtr = outPtr;
      inCnt = 10 - outCnt;
    
    innerloop:
    
      if (min > *inPtr)
      {
        minPtr = inPtr;
        min = *minPtr;
      }
    
      inPtr++;
      inCnt--;
    
      if (inCnt > 0)
        goto innerloop;
    
      tmp = *outPtr;
      *outPtr = min;
      *minPtr = tmp;
    
      outPtr++;
      outCnt++;
      if (outCnt < 10)
        goto outerloop;
    }
    
    void sortAsm(void)
    {
      char* rdi; // points to the boundary of the "outer loop"
      char* rsi; // points to the boundary of the "inner loop"
      char* r8; // holds the current minimum value
      char r9b; // holds the current minimum value
      char r10b; // temporary storage for character exchange
      long long rbx; // outer loop counter
      long long rax; // inner loop counter
    
      rdi = str; // initialize outer loop pointer
      rbx = 0; // initialize outer loop counter
    
    outerloop:
    
      r8 = rdi; // assume current element of partially sorted array is minimum,
      r9b = *r8; // save its index and value
    
      rsi = rdi; // initialize inner loop pointer
      rax = 10; // initialize inner loop counter
      rax -= rbx;
    
    innerloop:
    
      // compare the current small value with the value in [rsi]
      if (r9b > *rsi)
      {
        r8 = rsi; // save the new small value's index
        r9b = *r8; // save the new small value
      }
    
      rsi++; // move the inner loop pointer forward
      rax--; // decrement the inner loop counter
      if (rax > 0)
        goto innerloop;
    
      // When the inner loop is completed...
      // First, do the swap
      // to swap [r8] (target memory address) with [rdi] (outer array boundary)
      r10b = *rdi;
      *rdi = r9b;
      *r8 = r10b;
    
      rdi++; // move the outer loop pointer forward
      rbx++; // increment the outer loop counter
      if (rbx < 10)
        goto outerloop;
    }
    
    int main(void)
    {
      strcpy(str, strOriginal);
      printf("before sorting: %s\n", str);
      sortC1();
      printf("after sorting : %s\n\n", str);
    
      strcpy(str, strOriginal);
      printf("before sorting: %s\n", str);
      sortC2();
      printf("after sorting : %s\n\n", str);
    
      strcpy(str, strOriginal);
      printf("before sorting: %s\n", str);
      sortC3();
      printf("after sorting : %s\n\n", str);
    
      strcpy(str, strOriginal);
      printf("before sorting: %s\n", str);
      sortC4();
      printf("after sorting : %s\n\n", str);
    
      strcpy(str, strOriginal);
      printf("before sorting: %s\n", str);
      sortAsm();
      printf("after sorting : %s\n\n", str);
    
      return 0;
    }
    

    The output:

    before sorting: 8163045297
    after sorting : 0123456789
    
    before sorting: 8163045297
    after sorting : 0123456789
    
    before sorting: 8163045297
    after sorting : 0123456789
    
    before sorting: 8163045297
    after sorting : 0123456789
    
    before sorting: 8163045297
    after sorting : 0123456789
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Whilst learning the assembler language (in linux on a x86 architecture using the GNU
I'm a bit new to assembler, but I'm trying to lookup the parameters from
I'm trying to compile some given assembler files (.S) for Android using the NDK.
I'm trying to implement some inline assembler (in C/C++ code) to take advantage of
I've pretty much finished coding a SIC assembler for my systems programming class but
I am just testing and trying to learn how assembler works with C. So
I am trying to debug a function Reset_Handler() written in assembler (which I do
Using inline assembler [gcc, intel, c], how to check if the carry flag is
I'm starting with assembler under Linux. I have saved the following code as testasm.c
I got some assembler code that creates a formatted string using sprintf() : ...

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.