0% found this document useful (0 votes)
7 views6 pages

Asm 2

The document contains assembly code that implements a bubble sort algorithm on an array of integers. It measures the time taken to sort the array and prints the sorted array along with the time in ticks. The program also includes a subroutine for printing 16-bit numbers in hexadecimal format.

Uploaded by

lamgamo187
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views6 pages

Asm 2

The document contains assembly code that implements a bubble sort algorithm on an array of integers. It measures the time taken to sort the array and prints the sorted array along with the time in ticks. The program also includes a subroutine for printing 16-bit numbers in hexadecimal format.

Uploaded by

lamgamo187
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

org 100h

jmp start

; ===== D? LI?U =====

array db 14, 2, 9, 7, 3, 10, 5, 8, 1, 6

len equ 10

sorted_flag db ?

newline db 0Dh, 0Ah, '$'

msg_ticks db ' ticks (each ~55ms)', 0Dh, 0Ah, '$'

start_tick_low dw 0

start_tick_high dw 0

end_tick_low dw 0

end_tick_high dw 0

; ===== CHƯƠNG TR?NH CHÍNH =====

start:

xor ax, ax

mov ax, cs

mov ds, ax

; ==== Đ?c th?i gian b?t đ?u ====

mov ax, 0040h

mov es, ax

mov bx, es:[006Ch]

mov cx, es:[006Eh]


mov [start_tick_low], bx

mov [start_tick_high], cx

; === Bubble Sort có thoát s?m ===

mov cx, len - 1

outer_loop:

mov [sorted_flag], 1

mov si, offset array

mov dx, cx

inner_loop:

mov al, [si]

mov bl, [si+1]

cmp al, bl

jae no_swap

; Hoán đ?i n?u c?n

mov [si], bl

mov [si+1], al

mov [sorted_flag], 0

no_swap:

inc si

dec dx

jnz inner_loop
cmp [sorted_flag], 1

je sorted_done

dec cx

jnz outer_loop

sorted_done:

; === In m?ng đ? s?p x?p ===

mov cx, len

mov si, offset array

print_loop:

mov al, [si]

xor ah, ah

push cx

call print_number_16

pop cx

cmp cx, 1

jne print_space

mov ah, 09h

mov dx, offset newline

int 21h

jmp done_print
print_space:

mov ah, 02h

mov dl, ' '

int 21h

done_print:

inc si

dec cx

jnz print_loop

; ==== Đ?c th?i gian k?t thúc ====

mov ax, 0040h

mov es, ax

mov bx, es:[006Ch]

mov cx, es:[006Eh]

mov [end_tick_low], bx

mov [end_tick_high], cx

; ==== Tính s? ticks (DWORD) ====

mov ax, [end_tick_low]

sub ax, [start_tick_low]

mov bx, ax

mov ax, [end_tick_high]

sbb ax, [start_tick_high]


; In BX (ph?n th?p c?a tick)

mov ax, bx

call print_number_16

; In thông báo "ticks"

mov dx, offset msg_ticks

mov ah, 09h

int 21h

; K?t thúc

mov ah, 4Ch

int 21h

; ===== HÀM IN S? 16-BIT TRONG AX =====

print_number_16:

push ax

push bx

push cx

push dx

cmp ax, 0

jne .convert

mov dl, '0'

mov ah, 02h

int 21h

jmp .done
.convert:

xor cx, cx

mov bx, 10

.repeat:

xor dx, dx

div bx

push dx

inc cx

cmp ax, 0

jne .repeat

.print:

pop dx

add dl, '0'

mov ah, 02h

int 21h

loop .print

.done:

pop dx

pop cx

pop bx

pop ax

ret

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy