title: Assembly x86 TASM Sorting
body: <p>I am a begginer in Assembly language (TASM 86x) working on my first program assignment.
  It's not complicated in nature, however being new to this language I'm having a hard time figuring out a simple bubble sort.</p>
  
  <p>So far I have only programed witch C++, hardest part overall is to grasp the syntax.</p>
  
  <p>Task is to take any string (typed in by user) and rearrange it ascending by ASCII value (as in, if you type beda it should give abde)</p>
  
  <p>I'm not certain about my output, but that should come after the sort is done
  I'm confused, because <strong>it just allows me to input my string and then quits to the command prompt. Can't trace where I've made a mistake where it points to the end of the code prematurely.</strong> </p>
  
  <p>I would be very grateful if someone more experienced would take a look at my code and point me in the right direction and maybe even explain a thing or two to a newbie</p>
  
  <pre><code>.model small
  .stack 100h
  
  .data
  request     db 'Enter symbols:', 0Dh, 0Ah, '$'  
  
      buffer      db 100, ?, 100 dup (0)
  
  .code
  
  start:
      MOV ax, @data                   
      MOV ds, ax                      
  
  ; request
      MOV ah, 09h
      MOV dx, offset request
      int 21h
  
  ; read string                    ;reading string to buffer
      MOV dx, offset buffer           
      MOV ah, 0Ah                     
      INT 21h                         
      MOV si, offset buffer           
  
  
      INC si                        ;going from buffer size to actual length 
                                    ;of the string
      MOV cl, [si]              ;string length - loop counter
      mov ch, [si]                  ;string length - loop counter
      mov bl, [si]                  ;bl will be used to reset inner loop counter 
      DEC cl                        ;correcting the values, since count goes
      dec ch                        ; from 0 to n-1 instead of 1 to n
  
      inc si                        ;moving to strings first byte
  
  
  outer:                            ;outer loop
  
      dec ch                        ;decrease counter each pass
      jz ending                     ;when counter reaches 0 end program
      mov cl, bl                    ; reset inner loop counter value
  
  
  inner:                            ;inner loop
      mov al,byte ptr[si]           ;assigning byte(sybol) to al
      mov ah, byte ptr[si+1]        ;assigning following byte(symbol) to ah
      cmp al,ah                     ;compare the two
      jle after_switch              ;if the latter's value is higher, no need to switch
  </code></pre>
  
  <p><strong>problems with the switch, not sure if it will work right in assembly</strong></p>
  
  <pre><code>    mov bh, al           ;main problem-switching values, tried a few different   
      mov al, ah           ;ways of doing it (will show them below), but to no avail
      mov ah, bh           ;using familiar C syntax
  
      jmp output           ;outputing the value
  
  after_switch:         ;no switch needed
  </code></pre>
  
  <p><strong>somewhere in the outer switch there is supposed to be jump to output, however i cant figure out the way to include it without messing up the rest of the sequence</strong></p>
  
  <pre><code>    inc [si]              ;going to the next byte
      dec cl                ;decreasing inner loop counter
      jnz inner             ;back to the beginning of inner loop until counter reaches 0 (resetting in the outer loop)
      jmp outer             ;if counter reaches zero, get back to outer
  
  
  output:             ;outputting value from the very first bit 
      mov ah, 2
      mov dl, al          ;which after switch is supposed to be stored in al
      int 21h
      jmp inner           ;returning to inner loop to run next course of comparison
  
  ending:
      MOV ax, 4c00h               
      INT 21h                              
  end start
  </code></pre>
  
  <p>Previously tried methods of switch in inner loop</p>
  
  <pre><code>    mov al,[si+1]
      mov byte ptr[si+1],[si]
      mov byte ptr[si], al
  </code></pre>
  
  <p>returns illegal memory reference error, but this question has already been answered on this board in the past, found it.</p>
  
  <p>tried the same method, but utilizing the dx:di register</p>
  
  <pre><code>    mov al, byte ptr[si+1]
      mov dx:[di], [si]
      mov byte ptr[si+1], dx:[di]
      mov byte ptr[si], al
  </code></pre>
  
  <p>returns illegal override register error, couldn't find anything on it</p>