汇编语言:如何为输出创建两列(排序还是未排序)?

问题描述 投票:0回答:1

我已经用汇编语言编写了我的第一个程序,该程序从用户那里接收十个整数,每个都回显打印,使用插入排序对它们进行排序,然后应该打印出两个列表的表-未排序和已排序。我一直无法弄清楚如何生成表作为输出到屏幕。

该表必须由两列组成:按其未排序顺序的原始整数列表,以及从最小到最大的整数排序列表。该表的标题必须在每列的顶部包含一个标签。第一列的标题应为“未排序列表”,而第二列的标签应为“排序列表”。

我不确定如何执行此操作。谁能帮忙?

我的代码在下面完成:

;****** BEGIN MAIN PROGRAM ************************************
    DOSSEG
    .186
    .model large
    .stack 200h

;****** MAIN PROGRAM DATA SEGMENT *****************************
    .data
Prompt1  db  'Enter integer #$'
Prompt2  db  ': $'
Limit    dw  10         ;Array limit: 10 numbers
N        dw  10 dup(0)       ;ARRAY     
Count    dw  1          ;Loop counter
i        dw  1      ;Loop index for array 
j        dw  ?      ;Loop index for array 
MsgEcho  db  'The input was:$'
MsgUnsorted      db  'Unsorted array: $'
MsgSorted    db  'Sorted array: $'


;****** MAIN PROGRAM CODE SEGMENT *****************************
    .code
    extrn   PutStr: PROC, PutCrLf: PROC
    extrn   GetDec: PROC, PutDec: PROC

ProgramStart   PROC

; Initialize ds register to hold data segment address
    mov ax,@data
    mov ds,ax


; Call procedure Greet to print introductory messages to the user
    call    Greet        ; call subroutine to print greeting

; Print prompt message to the user
    mov dx,OFFSET Prompt1  ; point to the Prompt mesg
    mov ah,9         ; DOS print string function #
    int 21h      ; print string
   ; Print the Count value as part of prompt
    mov ax,Count     ; put parameter for subroutine PutDec in ax
    call    PutDec       ; print the decimal integer in ax
   ; Finish the prompt string
    mov dx,OFFSET Prompt2  ; point to the Prompt mesg
    mov ah,9         ; DOS print string function #
    int 21h      ; print string


; Input an integer from the user keyboard and assign it to N
    call    GetDec       ; integer from keyboard returned in ax
    mov [N],ax         ; store input value in memory location
    ;sub si,si
    mov si,0

; Print to verify the number was correctly input
  ; Print message
    mov dx,OFFSET MsgEcho  ; point to the output mesg
    mov ah,9         ; DOS print string function #
    int 21h      ; print string
  ; Print the input integer
    mov ax,[N]        ; put parameter for subroutine PutDec in ax
    call    PutDec       ; print the decimal integer in ax
    call    PutCrLf     ;new line character
    call    PutCrLf

    add si,2  ; for next element in the array
    inc Count
; Repeatedly prompt the user and input the an integer and print the
; integer, while the number of input integers is less than the limit.
; Also, find the maximum input number as they are input.

WHILE01:    ;  Count < Limit
    mov ax, Count       ; assign Count to ax
    cmp ax, Limit
    jnle    ENDWHL01
DO01:           ; Input integer in the array, print it, and update
   ; Print prompt message to the user
    mov dx,OFFSET Prompt1  ; point to the Prompt mesg
    mov ah,9         ; DOS print string function #
    int 21h      ; print string
   ; Print the Count value as part of prompt
    mov ax,Count     ; put parameter for subroutine PutDec in ax
    call    PutDec       ; print the decimal integer in ax
   ; Finish the prompt string
    mov dx,OFFSET Prompt2  ; point to the Prompt mesg
    mov ah,9         ; DOS print string function #
    int 21h      ; print string

  ; Input an integer from the user keyboard and assign it to N
    call    GetDec       ; integer from keyboard returned in ax
    mov [N+si],ax         ; store input values in an array


  ; Print to verify that the number was input correctly from
  ; keyboard and assigned to N correctly
    mov dx,OFFSET MsgEcho  ; point to the output mesg
    mov ah,9         ; DOS print string function #
    int 21h      ; print string

  ; Print the input integer
    mov ax,[N+si]      ; put parameter for subroutine PutDec in ax
    call    PutDec       ; print the decimal integer in ax
    call    PutCrLf


  ; Increment Count and repeat the loop
    add si, 2       ;next item in the array
    inc Count
    jmp WHILE01
; End of while loop
ENDWHL01:
    nop

    ;Print out the unsorted array
    mov dx, OFFSET MsgUnsorted
    mov ah,9
    int 21h
    call    PutCrLf

    sub si,si       ;initialize the array loop counter
    mov cx,Limit    ;initialize loop counter
WHILE02:            ;loop for printing the unsorted array
    mov ax, [N+si]
    call PutDec
    call    PutCrLf
    add si, 2       ;next item
    loop WHILE02

;SORTING THE ARRAY
    push Limit        ;passing by value
    push OFFSET N     ;passing by parameter
    call InsertionSort

;Print out the sorted array
    mov dx, OFFSET MsgSorted
    mov ah, 9
    int 21h
    call    PutCrLf

    sub si,si       ;initialize the array loop counter
    mov cx,Limit    ;initialize loop counter
WHILE03:            ;loop for printing the sorted array
    mov ax, [N+si]
    call PutDec
    call    PutCrLf
    add si, 2       ;next item
    loop WHILE03

; Exit to the operating system
    mov ah,4ch       ; DOS terminate program fn #
    int 21h
ProgramStart    ENDP

comment |
******* PROCEDURE HEADER **************************************
  PROCEDURE NAME : InsertionSort
  PURPOSE :  To sort an array using insertion sort algorithm
  INPUT PARAMETERS : Size of the array by value and the unsorted array by reference
  OUTPUT PARAMETERS or RETURN VALUE: Sorted array
  NON-LOCAL VARIABLES REFERENCED: None
  NON-LOCAL VARIABLES MODIFIED :None
  PROCEDURES CALLED :
    FROM iofar.lib: PutCrLf
  CALLED FROM : main program
|

.data
SORT_ARRAY   EQU  [bx]
.code
InsertionSort PROC      near

    ;SAVE REGISTERS
    pusha

    mov  bp,sp
    mov bx, [bp+18]     ;array
    mov cx, [bp+20]     ;array size
    mov si,2

forLoop:        ;dx = temp, si = i, di = j
    mov dx,SORT_ARRAY[si]  ;temp = N[i]
    mov di,si       ;j = i-1
    sub di,2
whileLoop:
    cmp dx,SORT_ARRAY[di]       ;temp < N[i]
    jge exit_whileLoop

    mov ax,SORT_ARRAY[di]       ;N[j+1] = N[j]
    mov SORT_ARRAY[di+2],ax
    sub di,2                    ;j = j - 1
    cmp di,0                    ;j >= 0
    jge whileLoop               
exit_whileLoop:
    mov SORT_ARRAY[di+2],dx     ;N[j=1] = temp
    add si,2                    ;i = i + 1
    dec cx
    cmp cx,1                    ; if cx = 1, done
    jne forLoop

;RESTORE REGISTERS
    popa
    ret   
InsertionSort ENDP

comment |
******* PROCEDURE HEADER **************************************
  PROCEDURE NAME : Greet
  PURPOSE :  To print initial greeting messages to the user
  INPUT PARAMETERS : None
  OUTPUT PARAMETERS or RETURN VALUE:  None
  NON-LOCAL VARIABLES REFERENCED: None
  NON-LOCAL VARIABLES MODIFIED :None
  PROCEDURES CALLED :
    FROM iofar.lib: PutCrLf
  CALLED FROM : main program
|
;****** SUBROUTINE DATA SEGMENT ********************************
    .data
Msgg1    db  'Program:   Sort the input stored in an array using Insertion sort $'
Msgg2    db  'Programmer: Ngoc Anh Thy Ly $'
Msgg3    db  'Date:       March 31, 2020 $'


;****** SUBROUTINE CODE SEGMENT ********************************
    .code
Greet   PROC    near

; Initialize ds register to hold data segment address
    mov ax,@data
    mov ds,ax

; Save registers on the stack
    pusha
    pushf

; Print name of program
    mov dx,OFFSET Msgg1 ; set pointer to 1st greeting message
    mov ah,9            ; DOS print string function #
    int 21h         ; print string
    call    PutCrLf

; Print name of programmer
    mov dx,OFFSET Msgg2 ; set pointer to 2nd greeting message
    mov ah,9            ; DOS print string function #
    int 21h         ; print string
    call    PutCrLf

; Print date
    mov dx,OFFSET Msgg3 ; set pointer to 3rd greeting message
    mov ah,9            ; DOS print string function #
    int 21h         ; print string
    call    PutCrLf

; Restore registers from stack
    popf
    popa

; Return to caller module
    ret
Greet   ENDP
end ProgramStart
assembly dos x86-16 dosbox
1个回答
0
投票
您有2个选项:
© www.soinside.com 2019 - 2024. All rights reserved.