Project #18794 - Assembly Language Programming

Write an assembler program that takes several words from standard input (keyboard, one word per input line) and outputs them sorted alphabetically, also one word per line.

The number of input words is not limited. Hence, your program should store the input words in a linked list and sort them directly in the list. You are not allowed to use any auxilary arrays for temporary storage in your code. You can store the strings dynamically and organize pointers to strings in a linked list and swap the pointers by, say, the bubble-sort method instead of swapping the corresponding strings.

You may use strcmp system call for comparing strings.

 

 

 

Exp.

 

SEGMENT .data
array:	DD 1, 3, 5, 6, 8, 10, 12, 15, 16
len:	EQU ($ - array) / 4
query:	DD 0
prompt:	DB "Enter query: ",0	
out1:	DB "Query found at index ",0
out2:	DB "Query is not found", 10, 0

SEGMENT .text
main:
        push    array           ; pass param by reference
        push    dword len   	; pass param by value
        call    printArray      ; print array 

	mov	edx, prompt
	call	WriteString	; print prompt
	call	ReadInt		; EAX = query
	mov	[query], eax

	push	dword array	; pass array
	push	dword 0		; left index
	push	dword len-1	; finish index
	push	dword [query]	; pass query
	call	binSearch	; EAX = query index
	
	cmp	eax, 0
	jl	.L1
	mov	edx, out1	; query is found
	call	WriteString	
	call	WriteInt	; index of query in array
	call	Crlf
	jmp	.L2
.L1	mov     edx, out2       ; query is not found
        call    WriteString
.L2	ret

;--------------------------------------------------------------
binSearch:
	push 	ebp
	mov	ebp, esp	; compose stack frame
		
	push	ebx		; save used regs
	push	ecx

	mov	eax, [ebp+16]	; left
	mov	ebx, [ebp+12]	; right
	cmp 	eax, ebx	; temination condition?
	jle 	.L1		; NO  - proceed with algo
	mov	eax, -1		; YES - return -1
	jmp	.L5		; exit

.L1:	add	eax, ebx	; eax = left + right
	shr	eax, 1		; eax = (left + right)/2
	mov	ecx, eax	; ecx = eax

	shl	ecx, 2		; ecx *= 4
	add	ecx, [ebp+20]	; ecx = address of a[mid]
	mov	ebx, [ecx]	; ebx = a[mid]
	cmp	ebx, [ebp+8]	; a[mid] ~ query
	je	.L5		; YES - return eax and exit	
	jl	.L4

.L3:	dec	eax		; eax = mid - 1
	push	dword [ebp+20]	; pass params to rec.call
	push	dword [ebp+16]  ; pass left
	push	eax		; pass right
	push	dword [ebp+8]	; pass query
	call	binSearch	; EAX = search result
	jmp	.L5

.L4:	inc	eax
        push    dword [ebp+20]  ; pass params to rec.call
        push    eax  		; pass left
        push    dword [ebp+12]  ; pass right
        push    dword [ebp+8]   ; pass query
        call    binSearch       ; EAX = search result

.L5:	pop	ecx
	pop	ebx	
	pop	ebp
	ret	16

;------------------------------------------------------
printArray:
        push    ebp
        mov     ebp, esp        ; compose stack frame

        push    eax             ; save used regs
        push    ecx
        push    esi

        mov     esi, [ebp+12]   ; set up array pointer
        mov     ecx, [ebp+8]    ; array length
.L:     mov     eax, [esi]      ; eax = arr[i]
        call    WriteInt        ; print it out
        mov     al, ' '         ; print a space
        call    WriteChar
        add     esi, 4
        loop    .L              ; loop back

        call    Crlf            ; start new line

        pop     esi
        pop     ecx
        pop     eax
        pop     ebp
        ret     8

 

Subject Computer
Due By (Pacific Time) 12/06/2013 11:59 pm
Report DMCA
TutorRating
pallavi

Chat Now!

out of 1971 reviews
More..
amosmm

Chat Now!

out of 766 reviews
More..
PhyzKyd

Chat Now!

out of 1164 reviews
More..
rajdeep77

Chat Now!

out of 721 reviews
More..
sctys

Chat Now!

out of 1600 reviews
More..
sharadgreen

Chat Now!

out of 770 reviews
More..
topnotcher

Chat Now!

out of 766 reviews
More..
XXXIAO

Chat Now!

out of 680 reviews
More..
All Rights Reserved. Copyright by AceMyHW.com - Copyright Policy