NOTE-The following Assembly Language Program has been prepared by our Assembly Language Programmers , just for demonstrating you the quality and comprehensiveness of our Assembly Language Homework Solutions and they do not constitute to any of our previous Assembly Language Assignment/Homework solution deliveries.
Sample Assembly Language Assignment
- Write an 80x86 assembly language program in EXE file format to do the following tasks:
- Open and read the contents of a file into memory (use at least 1 kB).
- Sort the list in numerical order using the bubble sort method.
- Display the results on the screen using the same format in (a).
- Save the output in the same file
- Uses some of the code from labs 2 and 3 as well as new code.
 
- Use the following procedures:
- MAIN: the main procedure.
- READ: reads the score file.
- DISPLAY: display the contents of the binary file.
- SORT: sort the entries in alphabetical order.
- ERROR: handles file errors.
- ADD_SCORE: adds a new score to the list
-  SAVE: consolidates the file to only contain the scores in the list, and saves them
 in order. It only saves the top 10 scores!
 
-  All the DOS function calls used for file I/O should be added as MACROs to your
 DOS.MAC file.
-  The high score list is organized as a singly-linked list. Each entry contains two fields:
- A pointer (offset address) to the next element. The data type is WORD (16-bit).
-  The text field contains a numerical score (ASCII) followed by a ' : ', followed by a
 name, and is terminated by 2 spaces (' '). The data type is BYTE.
- The list should only contain 10 scores. (The file may contain more than 10)
- Each name can be limited to 5 characters.
- The score must be able to range from 0 to 16777215.
-  You don’t explicitly declare a linked list in memory. You just declare a buffer
 space where the linked list will be read to, added to, sorted, and written from.
 
StringIn MACRO in
 mov ah,0AH
 mov dx,offset in
 int 21H
 ENDM
PrintC MACRO char
 mov dl,offset char
 mov ah,02H
 int 21h
 ENDM
PrintS MACRO string
 mov dx,offset string
 mov ah,09h
 int 21h
 ENDM
PushAll MACRO ;Save all the registers
 push ax
 push bx
 push cx
 push dx
 push di
 push si
 ENDM
PopAll MACRO ;Restore all the registers
 pop si
 pop di
 pop dx
 pop cx
 pop bx
 pop ax
 ENDM
Return MACRO
 mov ah,4CH
 int 21H
 ENDM
Create MACRO handle,filename
 mov cx, 0
 mov ah, 3ch
 mov dx, offset filename
 int 21h
 mov handle,ax
 call CheckError ;if carry set, there was an error
 ENDM
Open MACRO handle,filename
 mov al, 0 
 mov ah, 3dh
 mov dx, offset filename
 int 21h
 mov handle,ax
 ENDM
Len MACRO buff
 mov cx,2 ;take into account header
 mov bx, offset buff+2
 call calclen
 mov bx, offset buff
 mov [bx],cl
 ENDM
ReadFile MACRO handle,buff,size
 mov ah, 3fh
 mov bx, handle
 mov cx, size
 mov dx, offset buff
 int 21h
 call CheckError ;if carry set, there was an error
 ENDM
WriteFile MACRO handle,buff,size
 mov ah, 40h
 mov bx, handle
 mov dx, offset buff
 mov cx, size
 int 21h
 call CheckError ;if carry set, there was an error
 ENDM
Close MACRO handle
 mov bx, handle
 mov ah, 3eh
 int 21h
 call CheckError ;if carry set, there was an error
 ENDM
InChar MACRO
 mov ah,1H
 int 21H
 ENDM
.MODEL small
;FILENAME: Lab4.asm
 ;FILE FORMAT: EXE
 include dos.mac
stack_seg SEGMENT stack
 db 100 dup(?)
 stack_seg ENDS
;EXTRA_SEG SEGMENT EXTRA
 ;DB 100 DUP(?)
 ;EXTRA_SEG ENDS
data_seg SEGMENT 'DATA'
 ;file MUST be at the start of data segment
 ;since it replicates the contents of the file
 ;format of data
 ;dw pointer to next (or 0 at end)
 ;db score ':' name ' ' (2 spaces)
 head db 2,0 ;first entry
 scr1 db ?,?,"998:JEANE "
 scr2 db ?,?,"999:BILLY "
 scr3 db ?,?,"16777215:DAVID "
 db 0,0 ;end of sample data
 endinit db 4096 dup(0)
 endoffile db 0
hs db 0DH,0AH,"The high scores are: ",0DH,0AH,'$'
 prmpt db 0DH,0AH, "Do you want to:",0DH,0AH
 db "a. Add a new score to the list?",0DH,0AH
 db "b. Exit ",0DH,0AH
 db '$'
errprmpt db 0DH,0AH, "Choice unacceptable. $"
 fileerr db 0DH,0AH, "Problem with file. $"
filename db "score.bin",0
handle dw 0
 topten dw 0
 
 endline db 10,13,'$'
 char db ?
 nmin db 6,?,6 dup(?) ;6 characters
 scrin db 9,?,9 dup(?) ;9 characters
nmprmpt db 0DH,0AH, "Enter Name: $"
 scrprmpt db 0DH,0AH, "Enter Score: $"
bye db 0DH,0AH, "Bye $"
err02 db "File not found$"
 err03 db "Path not found$"
 
 errcode db ?
 swapped db ?
 tmp db ?
 data_seg ENDS
code_seg SEGMENT para 'CODE'
 assume CS:code_seg, DS:data_seg, SS:stack_seg
main PROC far
 mov ax,data_seg
 mov ds,ax
 mov ax,stack_seg
 mov ss,ax
 mov ax,data_seg ;extra_seg
 mov es,ax ;initialize segments
call read ;read file (create if not present)
 start:
 prints hs
over:
 call sort ;sort by score
 call displayfile ;display names
 prints prmpt
 InChar
 mov char,al
 cmp char,'a'
 je @a
 cmp char,'b'
 je @b
 jne @x
@a:
 call addScore ;add new entry
jmp start
 @b:
 prints bye
 jmp done
@x:
 prints errprmpt
 jmp over
done:
 call save
 .exit
 main ENDP
save PROC near
 mov bx,offset head
 mov ax,[bx]
 mov bx,ax ;go to 1st entry
 mov cx,10 ;top 10 only
 saveloop:
 mov ax,[bx]
 and ax,ax
 je endfile ;we reached 0
 add ax,offset head
 mov bx,ax
 dec cx
 je endfile ;maximum of 10 entries
 jmp saveloop
 endfile:
 mov word ptr [bx],0 ;if we have more than 10, stop here
 sub word ptr bx, offset head
 add bx,2 ;to account for the 0 at the end of file
 mov topten,bx
 
 create handle,filename ;create dummy file
 WriteFile handle,head,topten
 close handle
 RET
 SAVE ENDP
;bx points to 1st pointer
 ;di points to 2nd pointer
 ;[bx] should be larger than [di]
 ;returns ax != 0 if swap, else ax = 0
 compare PROC near
 push bx
 push di
 add bx,2
 add di,2 ;skip pointer
 ;First find longest number, it must be larger
 ;Search for :
 mov cl,':'
 .findcolon:
 mov al,[bx]
 mov dl,[di]
 inc bx
 inc di
 cmp al,cl
 je .len ;possible the same length
 cmp dl,cl
 JE .NOSWAP
 JMP .FINDCOLON
 .LEN:
 CMP DL,CL
 JNE .SWAP
 ;Must be the same length
 POP DI
 POP BX
 PUSH BX
 PUSH DI
 ;Now we need the data again
 ADD BX,2
 ADD DI,2
 .CMPLOOP: 
 MOV AL,[BX]
 MOV DL,[DI]
 CMP AL,CL
 JE .NOSWAP ;Reached : must be the same number
 INC BX
 INC DI
 CMP AL,DL
 JC .SWAP
 JMP .CMPLOOP
 .NOSWAP:
 POP DI
 POP BX
 MOV AX,0 ;No Swap
 RET
 .SWAP:
 POP DI
 POP BX
 MOV AX,1
 RET
 COMPARE ENDP 
 
 READ PROC NEAR
 OPEN handle,filename
 ;UNABLE TO FIND FILE, CREATE IT
 JC NOFILE
 ReadFile handle,head,endoffile-head
 CLOSE handle
 RET
 
 READ ENDP
NOFILE PROC NEAR
 LEN scr1 ;Find the length
 LEN scr2
 LEN scr3
 mov al,[scr1] ;Calculate links
 add al,2
 mov [scr1],al
 mov bl,[scr2]
 add al,bl
 mov [scr2],al ;for each score
 mov bl,[scr3]
 add al,bl
 mov [scr3],al ;using length
 
 CREATE handle,filename ;create dummy file
 WriteFile handle,head,endinit-head
 CLOSE handle
 RET
 NOFILE ENDP
CheckError:
 jc Error ;If carry flag set, then error
 ret
ERROR PROC NEAR
 mov errcode,al
 PRINTS fileerr
 cmp errcode,2
 jne noterr2
 PRINTS err02
 noterr2:
 cmp errcode,3
 jne noterr3
 PRINTS err03
 noterr3:
 JMP done
 ERROR ENDP
DISPLAYFILE PROC NEAR
 mov bx,offset head
 mov ax,[bx]
 mov bx,ax ;go to 1st entry
 displayloop:
 mov ax,[bx]
 and ax,ax
 je enddisplay ;we reached 0
 add ax,offset head
 push bx ;save current pointer
 add bx,2 ;skip pointer value
 mov dx,bx ;dx = start of string
 ;find the end of string and add $ to it
 .findend:
 mov ah,[bx]
 inc bx
 cmp ah,' '
 jne .findend ;loop until we find a space
 mov ah,[bx]
 cmp ah,' ' ;was it double space
 jne .findend ;nope
 mov byte ptr [bx],'$' ;add end of string
 MOV AH,09 
 INT 21h
 mov byte ptr [bx],' ' ;remove end of string
 pop bx ;restore current pointer
 PRINTS endline
 mov ax,[bx]
 mov bx,ax
 
 jmp displayloop
 enddisplay:
 ret
 DISPLAYFILE ENDP
CalcLen PROC NEAR
 mov al,[bx]
 inc cx
 inc bx
 cmp al,' ' ;ends with double space
 jne CalcLen
 mov al,[bx]
 inc cx
 inc bx
 cmp al,' ' ;ends with double space (this is 2nd)
 jne CalcLen
 ret
 CalcLen ENDP
;void sort()
 ;{
 ; bool swapped = true;
 ; while (swapped)
 ; {
 ; swapped = false;
 ; Item *prev = &root;
 ;
 ; Item *curr = root.next;
 ; Item *next = curr->next;
 ;
 ; while (next)
 ; {
 ; if (next->score > curr->score)
 ; {
 ; prev->next = next;
 ; curr->next = next->next;
 ; next->next = curr;
 ; swapped = true;
 ; }
 ; prev = curr;
 ; curr = next;
 ; next = next->next;
 ; }
 ; }
 ;}
SORT PROC NEAR
 ; bool swapped = false;
 mov [swapped],0
 ; Item *prev = &root;
 mov si,offset head
 ; Item *curr = root.next;
 mov bx,[si]
 ; Item *next = curr->next;
 mov di,[bx]
 ; while (next)
 .while:
 ; if (next->score > curr->score)
 PushAll
 call compare
 mov [tmp],al
 PopAll
 mov al,[tmp]
 and al,al
 je .nosortswap
 ; prev->next = next;
 mov [si],di
 ; curr->next = next->next;
 mov ax,[di]
 mov [bx],ax
 ; next->next = curr;
 mov [di],bx
 ; swapped = true;
 mov [swapped],1
 jmp sort ;pointers are modified, restart
 .nosortswap:
 ; prev = curr;
 mov si,bx
 ; curr = next;
 mov bx,di
 ; next = next->next;
 mov ax,[di]
 ; while (next)
 mov di,ax
 mov ax,[di]
 and ax,ax
 jne .while
 ; while (swapped)
 mov al,[swapped]
 and al,al
 ; jne sort
 ret 
 
 SORT ENDP
ADDSCORE PROC NEAR
 PRINTS nmprmpt
 STRINGIN nmin
 PRINTS scrprmpt
 STRINGIN scrin
 mov bx,offset head
 addscoreloop:
 mov ax,[bx]
 and ax,ax
 je addscoreend ;we reached 0
 add ax,offset head 
 mov bx,ax
 jmp addscoreloop
 addscoreend:
 mov ax,bx
 mov cx,0
 mov cl,[nmin+1]
 add ax,cx
 mov cl,[scrin+1]
 add ax,cx
 add ax,5 ;for : 2 spaces and 0
 mov [bx],ax
 add bx,2
 mov cx,offset scrin
 call copystr
 mov byte ptr [bx],':'
 inc bx
 mov cx,offset nmin
 call copystr
 mov byte ptr [bx],' '
 inc bx
 mov byte ptr [bx],' '
 inc bx
 mov byte ptr [bx],0
 inc bx
 mov byte ptr [bx],0
 
 RET
 ADDSCORE ENDP
 
 ;copys a string from cx to bx, cx[1] has the length 
 copystr proc near
 mov di,cx
 inc di ;size of buffer 
 mov dl,[di]
 inc di ;count
 .loop:
 mov al,dl
 je .ret
 mov al,[di]
 inc di
 mov [bx],al
 inc bx
 dec dl
 jmp .loop
 .ret: ret
 copystr endp
CODE_SEG ENDS
 END MAIN