Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

sorting array

Please help me with this:

1. An assembly program that fills an array with a random sequence of signed integers. Display array, sort it and redisplay it.

Secondly write a procedure that performs a binary search for a single value in array of intergers. The array must first sorted in either ascending or descending order. Procedure should use loop that repeats until it discovers that the value is not in the list being searched.

Comments

  • emu8086emu8086 Member Posts: 125
    Hope this helps:
    This code is taken from emu8086 pack,
    it requires "emu8086.inc":
    http://www.programmersheaven.com/search/download.asp?FileID=20562

    [code]

    TITLE Simple Sort

    ; AUTHOR Yuri Margolin
    ; http://mysoft.s5.com/
    ;
    ; DATE 23/01/01
    ; VERSION 1.02
    ; FILE sort.asm

    ; THIS PROGRAM INPUTS 3 NUMBERS AND
    ; SORTS THEM FROM LARGEST TO SMALLEST

    #MAKE_EXE#

    ; contains some common
    ; procedures and macros,
    ; (definition is done in the bottom
    ; of this file):
    INCLUDE 'emu8086.inc'


    DSEG SEGMENT 'DATA'
    CR EQU 0Dh
    LF EQU 0Ah
    DOLLAR EQU '$'
    NEW_LINE DB LF, CR, DOLLAR
    MSG1 DB 'ENTER first value (-32768..32767)!'
    DB LF, CR, DOLLAR
    MSG2 DB LF, CR, 'ENTER second value (-32768..32767)!'
    DB LF, CR, DOLLAR
    MSG3 DB LF, CR, 'ENTER third value (-32768..32767)!'
    DB LF, CR, DOLLAR
    MSG4 DB CR, LF, CR, LF, 'After sorting:', DOLLAR
    MSG5 DB CR, LF, 'num1 = ', DOLLAR
    MSG6 DB CR, LF, 'num2 = ', DOLLAR
    MSG7 DB CR, LF, 'num3 = ', DOLLAR
    num1 DW ?
    num2 DW ?
    num3 DW ?
    DSEG ENDS

    SSEG SEGMENT STACK 'STACK'
    DW 100h DUP(?)
    SSEG ENDS

    CSEG SEGMENT 'CODE'

    ;****************************************

    START PROC FAR

    ; Prepare for return to OS:
    PUSH DS
    MOV AX, 0
    PUSH AX
    ; Set segment registers:
    MOV AX, DSEG
    MOV DS, AX
    MOV ES, AX

    ; Clear the screen:
    CALL clear_screen

    ; Position the cursor at row=3 and column=0
    GOTOXY 0, 3


    ; Ask for first number:
    LEA DX, MSG1
    CALL PUTS ; Display the message.
    CALL SCAN_NUM ; Input the number into CX.
    MOV num1, CX

    ; Ask for second number:
    LEA DX, MSG2
    CALL PUTS ; Display the message.
    CALL SCAN_NUM ; Input the number into CX.
    MOV num2, CX

    ; Ask for third number:
    LEA DX, MSG3
    CALL PUTS ; Display the message.
    CALL SCAN_NUM ; Input the number into CX.
    MOV num3, CX

    ; Sorting:
    MOV BX, num1
    MOV CX, num2
    CALL SORT ; exchange if BX<CX
    MOV num1, BX
    MOV num2, CX

    MOV BX, num2
    MOV CX, num3
    CALL SORT ; exchange if BX<CX
    MOV num2, BX
    MOV num3, CX

    MOV BX, num1
    MOV CX, num2
    CALL SORT ; exchange if BX<CX
    MOV num1, BX
    MOV num2, CX

    ; Print the result:
    LEA DX, MSG4
    CALL PUTS

    LEA DX, MSG5
    CALL PUTS
    MOV AX, num1
    CALL PRINT_NUM ; print AX.

    LEA DX, MSG6
    CALL PUTS
    MOV AX, num2
    CALL PRINT_NUM ; print AX.

    LEA DX, MSG7
    CALL PUTS
    MOV AX, num3
    CALL PRINT_NUM ; print AX.

    LEA DX, NEW_LINE
    CALL PUTS

    RET
    START ENDP

    ;***********************************

    ; Displays the message (DX-address),
    ; uses DOS function to print:
    PUTS PROC NEAR
    PUSH AX
    MOV AH, 09h
    INT 21h
    POP AX
    RET
    PUTS ENDP

    ;************************************

    ; if BX < CX then exchanges them
    ; (works with signed numbers)
    SORT PROC NEAR
    CMP BX, CX
    JGE compared
    XCHG BX, CX
    compared:
    RET
    SORT ENDP

    ;************************************

    ; Definition of some common procedures
    ; from emu8086.inc:

    DEFINE_CLEAR_SCREEN
    DEFINE_PRINT_STRING

    DEFINE_SCAN_NUM

    DEFINE_PRINT_NUM
    DEFINE_PRINT_NUM_UNS

    ;*************************************

    CSEG ENDS
    END START


    [/code]
  • Shawn CarterShawn Carter Member Posts: 0

    ________ [ http://forcoder.org ] free ebooks and video tutorials about / C#, Swift, Visual Basic .NET, Ruby, Delphi, Visual Basic, C, Go, Assembly, PHP, PL/SQL, R, Perl, Scratch, C++, MATLAB, Python, Objective-C, JavaScript, Java Fortran, Awk, Ada, LabVIEW, Erlang, Prolog, COBOL, Crystal, FoxPro, Lua, Clojure, Logo, F#, Scala, Rust, D, Apex, ML, Hack, Transact-SQL, Dart, VBScript, Kotlin, ABAP, Alice, Lisp, SAS, Bash, Julia, Scheme / ___________

Sign In or Register to comment.