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 [login to view URL]
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 [login to view URL]
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
WOW! It seems that this project was posted by me... As I can program wide variety of controllers including low end (8051,PIC,AVR,MSP430) and high end ARM Cortex-M core based (STM8,STM32,Kinetis controllers by Freescale) etc etc... I am a professional R&D "Embedded Design Engineer" which has successfully completed many embedded projects and many of them are in progress...
Have a look @ my short intro below:
Electrical Engineer (Gold-Medalist) from the oldest and the most reputable engineering institute of Pakistan (UET, Lahore). Done my internship @ US based R&D embedded company “Mentor Graphics” and currently working as Professional “Embedded Design Engineer” @ Asia’s #1 ranked R&D company “MicroTech Industries”…
Current Research Project:
Currently researching on MSP430 and Cortex M4 cores in order to find a solution for robust way of “Remote Firmware Up-gradation”. This research project is intended to save time and money needed for manual firmware up-gradation. “MicroTech Industries” will be able to save millions of dollars per annum by having this feature embedded into its “Smart Energy Meters”
Looking for your positive reply soon :)
Over 15 years programming INtel with 8080 (8 bits), 8086 and Windows Kernele Model.
I have experience with MASM compiler, FASM,
Debugger like IDA, OllyDBG and the old DEBUG
And PC architeture as BIOS, INterrupt and WIn32 API
Hello!
Some time ago I've implemented a hash table in x86 assembler, storing and sorting some words will be a piee of cake ;)
I'm a CS student as well.