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

  • SEARCH
  • Home
  • 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 8662379
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T16:43:29+00:00 2026-06-12T16:43:29+00:00

I am having problem on how exactly should i use loop to get the

  • 0

I am having problem on how exactly should i use loop to get the desire output in this program,

What i want to do is to take input any number from the user and then sort that number in descending order,

I tried my best here to explain every step of the code in the comment.

here is my code,

STSEG SEGMENT
        DB 64 DUP(?)
STSEG ENDS

DTSEG SEGMENT
        SNAME    DB 24 DUP("$")


DTSEG ENDS

CDSEG SEGMENT
    MAIN PROC
    ASSUME CS:CDSEG, DS:DTSEG, SS:STSEG

    MOV AX,DTSEG
    MOV DS,AX
    MOV ES, AX     ;ES:DI


    MOV DX, OFFSET STRNG1
    MOV AH,09
    INT 21H

    XOR DX,DX


      MOV BYTE PTR SNAME, 40
      MOV DX, OFFSET SNAME  

      MOV AH, 0AH
      INT 21H


      PUSH DX ;Hold the input number in a stack until we clear the screen and set the cursor
      ; The clear screen and cursor position code is here which i didn't really mention.

      ;What we need to do now is to compare first number to each other number and store the greatest


 of two on first position.


      MOV BX,DX ;Copy un-sorted number to BX, 

      MOV AX,BX[1] ;the length of the number which is stored on the first position of the string
      XOR AH,AH      ;Empty AH
      MOV CL,AL  ;MOVE AL into CL for looping 6 times
      SUB CL,1

      MOV SI,02H ;the number is stored in string array from the 2nd position


;Suppose the input number is the following,
      ;[6][3][9][1][8][2][6]

      ;this is how it should work,


      ; Loop 6 times , CX = 6
      [7][6][3][9][1][8][2][6] ; 7 is length of the number which is already copied in CX above.
      ; but we need 6 iterations this is why we subtract 1 from CL above.

      ; 6 > 3 ? 
      ; Yes, then BX[SI] = 6 and BX[SI+1] = 3
      ; 6 > 9 ?
      ; NO, then BX[SI] = 9  and BX[SI+2] = 6
      ; 9 > 1   
      ; Yes, then BX[SI] = 9 and BX[SI+3] = 1
      ; 9 > 8
      ; Yes, then BX[SI] = 9 and BX[SI+4] = 8 
      ; 9 > 2
      ; Yes, then BX[SI] = 9 and BX[SI+5] = 2 
      ; 9 > 6
      ; Yes, then BX[SI] = 9 and BX[SI+6] = 6

; After first iteration the incomplete sorted number is,
;[9][3][6][1][8][2][6]

  ;Similarly here i need to loop 5 times now by comparing the 2nd number which is a 3 with all ;the number after it and then loop 4 times then 3 times then two times and then 1 time.


     L1: 
        ;Loop 1 must iterate 6 time for the supposed input number, 
;but i couldn't be able to write the proper code as i always get out of registers. kindly help me out   

        L2:


        LOOP L2


     Loop L1

Kindly help me with the nested loop where i’ve stucked.

  • 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-12T16:43:30+00:00Added an answer on June 12, 2026 at 4:43 pm

    The loop instruction uses the cx register.

    So you must either preserve cx for the outer loop with e.g. push cx (before L2🙂 and pop cx (after loop L2):

        mov  cx,5
    L1:
        push cx
        mov  cx,6
    L2:
        . . . do stuff inner
        loop L2
        pop  cx
        . . . do stuff outer 
        loop L1
    

    or remember that loop is roughly equal to dec cx jnz, so do e.g.

        mov  dx,5
    L1:
        mov  cx,6
    L2:
        ... do stuf inner
        loop L2
        .. do stuff outer
        dec  dx
        jnz  L1
    

    Possible off-by-one errors are intended and meant as an exercise for the reader 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am having problem to get a numerical value for this expression where I
I'm asking this question despite having read similar but not exactly what I want
I am having a problem understanding how array.sort{ |x,y| block } works exactly, hence
Im having problem in my UIscrollView ,this is what I have done: Whenever a
I asked a similar question yesterday but still having a slight problem on output.
Having problem with the middle Div not expanding to the width http://acs.graphicsmayhem.com/images/middiv.jpg Ok, how
I am having problem using mvc:resources in spring 3.1 configuration. Initially i was working
I am having problem with previewing custom performance counters with PerflibV2. Performance Monitor shows
I'm having problem with datagrid view. I have attached an image with the code
I am having problem with drawing multiple lines during repaint. The code is as

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.