assembly 汇编代码,用于将集合中的每个元素彼此进行比较

ryoqjall  于 2023-06-30  发布在  其他
关注(0)|答案(1)|浏览(129)

相应地比较位于两个不同存储器段(DS-0,ES-1)中的两组十六进制值。请编写一个汇编代码来比较集合中的每个元素,然后将较大的元素保存在第三个内存段中(请自行选择)。集合如下所示。
第一组:0x 3B 0x 4C 0x 73 0xE 5 0x 6D 0x 8B 0x 6C 0x 17 0x 6 E 0x 41第二组:0x3B 0x52 0x98 0x67 0x1F 0xAE 0x36 0xC9 0x70 0xED
比较每个集合并在第三个内存段中保存更大的值

v8wbuo2f

v8wbuo2f1#

repz cmpsb在这里工作,但我不知道在这个上下文中使用这个指令是否正确。cmp可能更好。还有太多的inc/dec si/di指令...
----更新1
好了,现在最大值被复制到第三段的data_set_3,但程序显示的是ascii代码而不是十六进制/十进制的值。

`

stack SEGMENT stack     ;stack segment
    db 128 dup (?)
    stack_top   db ?    ;stack_top pointer to stack
stack ENDS

ds_data SEGMENT         ;ds segment

   data_set_1       db 3bh,4ch,73h,0e5h,6dh, 8bh,6ch,17h,6eh,41h
                       ;3bh,52h,98h, 67h,1fh,0aeh,36h,17h,70h,0edh
    position         db "Position ",'$'
    position_offset  db 0
     set_1            db "Data Set 1   ",'$'    
     set_2            db "Data Set 2   ",'$'    
     set_3            db "Data Set 3   ",'$'             
     
    elements_same    db " - Value 1 = Value 2",'$'
    elements_less    db " - Value 1 < Value 2",'$'
    elements_greater db " - Value 1 > Value 2",'$'
 
    result_msg db "Result - Data in 3rd segment",'$'
 
    newline db 13,10,'$'   

ds_data ENDS

es_data SEGMENT             ;es segment

    data_set_2 db 3bh,52h,98h,67h,1fh,0aeh,36h,17h,70h,0edh

es_data ENDS

result_data SEGMENT         ;result segment
    data_set_3   db 10 dup(?)
    set_3_offset dw 0
 
result_data ENDS

code SEGMENT

start:
    ;Set up segments
    mov ax, seg stack
    mov ss, ax
    mov sp, offset stack_top
  
    mov ax, seg ds_data
    mov ds, ax
  
    mov ax, seg es_data
    mov es, ax  
  
    mov si, offset ds:data_set_1    ;offset to source string
    mov di, offset es:data_set_2    ;offset to destination string

   cld    ; clear direction flag, increase adresses
  
   mov cx,10    

   mov ah,9    ;new line
   mov dx,offset newline
   int 21h        

   check_elements:              ; compare 10 elements one by one
       push cx
      
       mov cx,1         
       repz cmpsb               ; compare ds:[si] to es:[di]
                            ; set flags
                            ; values same ZF = 1
                            ; src value is less than destination value CF = 1
                            ; src value is greater than destination value CF = 0, ZF = 0

       pushf                    ;push flags on stack
    
       call text_position
    
       popf             ;pop flags
;----------------------------------------------     
       ja src_greater_than_dest     ;src > dest
       jb src_less_than_dest            ;src < dest        
       ;else src = dest

       call text_src_equals_dest
       dec si
       dec di
    
       push es  
       mov ax, seg result_data
       mov es, ax   

       mov bx, offset es:data_set_3         ;offset to result string    
       add bx,es:[set_3_offset]             ;position in result string
       mov al,  [si]                            ;copy value to result string
       mov es:[bx],al
       inc es:[set_3_offset]                    ;next position in result string
       pop es           
        
       inc si
       inc di
    
       jmp check_element_done
  
       src_greater_than_dest:           
        
           call text_src_greater_dest

           dec si
           dec di       
        
           push es  
           mov ax, seg result_data
           mov es, ax   

           mov bx, offset es:data_set_3         ;offset to result string    
           add bx,es:[set_3_offset]             ;position in result string
           mov al,  [si]                            ;copy value to result string
           mov es:[bx],al
           inc es:[set_3_offset]                    ;next position in result string
           pop es           
        
           inc si
           inc di           
    
           jmp check_element_done

       src_less_than_dest:          
        
           call text_src_less_dest
        
           dec si
           dec di   
           mov al,  es:[di]                             ;copy value to result string
                    
           push es  
           push ax
           mov ax, seg result_data
           mov es, ax   

           mov bx, offset es:data_set_3         ;offset to result string    
           add bx,es:[set_3_offset]             ;position in result string
           pop ax
           mov es:[bx],al
           inc es:[set_3_offset]                    ;next position in result string
           pop es           
        
           inc si
           inc di           
        
       check_element_done:
           pop cx
           inc ds:[position_offset]

   loop check_elements

;Print data_set_1
mov ah,9                    ; new line
mov dx,offset newline
int 21h     
        
mov ah,9                    ; print "Data Set 1   "
mov dx,offset set_1 
int 21h     

mov cx,10   
mov bx, offset data_set_1
    
Show_data_set_1:
    mov ah,2
    mov dl,[bx] 
    int 21h
    
    mov ah,2
    mov dl,' '
    int 21h
    inc bx
loop Show_data_set_1    

;Print data_set_2
mov ah,9                    ; new line
mov dx,offset newline
int 21h     
        
mov ah,9                    ; print "Data Set 2   "
mov dx,offset set_2 
int 21h     

mov cx,10               
mov bx, offset es:data_set_2
    
Show_data_set_2:
    mov ah,2
    mov dl,es:[bx]  
    int 21h
    
    mov ah,2
    mov dl,' '
    int 21h
    inc bx
loop Show_data_set_2

;Print data_set_3
mov ah,9                    ; new line
mov dx,offset newline
int 21h     
        
mov ah,9                    ; print "Data Set 3   "
mov dx,offset set_3
int 21h                 

mov cx,10
mov ax, seg result_data ;change segment to result_data
mov es, ax              
mov bx, offset es:data_set_3
    
Show_data_set_3:
    mov ah,2
    mov dl,es:[bx]  
    int 21h
    
    mov ah,2
    mov dl,' '
    int 21h
    inc bx
loop Show_data_set_3

mov ah,9                    ; new line
mov dx,offset newline
int 21h     

end_program:     
    ; Terminate program
    mov ax, 4C00h
    int 21h
;--------------------------
text_position proc
    mov ah,9                        ; print "Position"
    mov dx,offset position
    int 21h
        
    mov ah,2
    mov dl,ds:[position_offset]     ; print position number
    add dl,30h
    int 21h
    ret
text_position endp

text_src_equals_dest proc
    mov ah,9                        ; print "Src = Destination"
    mov dx,offset elements_same
    int 21h     
        
    mov ah,9                        ; new line
    mov dx,offset newline
    int 21h      
    ret
text_src_equals_dest endp

text_src_greater_dest proc
    mov ah,9                    ; print "Src > Destination"
    mov dx,offset elements_greater
    int 21h     
        
    mov ah,9                    ; new line
    mov dx,offset newline
    int 21h      
    ret
text_src_greater_dest endp

text_src_less_dest proc
        mov ah,9                    ; print "Src < Destination"
        mov dx,offset elements_less
        int 21h     
        
        mov ah,9                    ; new line
        mov dx,offset newline
        int 21h      
    ret
text_src_less_dest endp

code ENDS
END start

`

相关问题