Assembly language sample programs

Hani Al-Banai's photo
Cheksum program startup

;-----------------------------------------------------;
; Title : Final Project                               ;
; COS 230                                             ;
; Programmer : Hani AL Banai                          ;
;                                                     ;
; This program is to simulate Traffic Light System    ;
; It will display three bars of three different       ;
; colors , a delay is going to control the time each  ;
; bar will be visible                                 ;
;-----------------------------------------------------;
.model small
.stack 100h

.code
main proc
      mov ax,@data
      mov ds,ax

      mov si,3           ; counter
bars: dec si

     mov cx,8555         ; delay
 L1: push cx             ;
     mov cx,2            ;
        L2:push cx       ;
        mov cx,8555      ;
           L3:push cx    ;
              pop cx     ;
           loop L3       ;
        pop cx           ;
        loop L2          ;
     pop cx              ;
     loop L1             ;

     mov ah,6            ; scroll window down
     mov al,0            ; all lines
     mov ch,2            ; upper left row for bar 1
     mov cl,11           ; upper left column for bar 1
     mov dh,22           ; lower right row for bar 1
     mov dl,21           ; lower right column for bar 1
     mov bh,01000000b    ; red background color
     int 10h             ; do it now

     mov cx,8555         ; delay
 L4: push cx
     mov cx,2
        L5:push cx
        mov cx,8555
           L6:push cx
              pop cx
           loop L6
        pop cx
        loop L5
     pop cx
     loop L4

     mov ah,6           ; scroll window down
     mov al,0           ; all lines
     mov ch,0           ; upper left row for whole window
     mov cl,0           ; upper left column for whole window
     mov dh,24          ; lower right row for whole window
     mov dl,79          ; lower right column for whole window
     mov bh,00000000b   ; back background color
     int 10h            ; do it now

     mov ah,6           ; scroll window down
     mov al,0           ; all lines
     mov ch,2           ; upper left row for bar 2
     mov cl,31          ; upper left column for bar 2
     mov dh,22          ; lower right row for bar 2
     mov dl,41          ; lower right column for bar 2
     mov bh,01111111b
     int 10h            ; do it now

     mov cx,8555        ; delay
 L10: push cx           ;
     mov cx,2           ;
        L11:push cx     ;
        mov cx,8555     ;
           L12:push cx  ;
              pop cx    ;
           loop L12     ;
        pop cx          ;
        loop L11        ;
     pop cx             ;
     loop L10           ;

     mov ah,6           ; scroll window down
     mov al,0           ; all lines
     mov ch,0           ; upper left row for whole window
     mov cl,0           ; upper left column for whole window
     mov dh,24          ; lower right row for whole window
     mov dl,79          ; lower right column for whole window
     mov bh,00000000b   ; back background color
     int 10h            ; do it now

     mov ah,6           ; scroll window down
     mov al,0           ; all lines
     mov ch,2           ; upper left row for bar 3
     mov cl,51          ; upper left column for bar 3
     mov dh,22          ; lower right row for bar 3
     mov dl,61          ; lower right column for bar 3
     mov bh,00100100b   ; green background color
     int 10h            ; do it now

     mov cx,8555        ; delay
 L7: push cx            ;
     mov cx,2           ;
        L8:push cx      ;
        mov cx,8555     ;
           L9:push cx   ;
              pop cx    ;
           loop L9      ;
        pop cx          ;
        loop L8         ;
     pop cx             ;
     loop L7            ;

     mov ah,6           ; scroll window down
     mov al,0           ; all lines
     mov ch,0           ; upper left row for whole window
     mov cl,0           ; upper left column for whole window
     mov dh,24          ; lower right row for whole window
     mov dl,79          ; lower right column for whole window
     mov bh,00000000b   ; back background color
     int 10h            ; do it now

     cmp si,0           ; check if the counter reached zero
     jne bars           ; if not repeat

     mov ax,4c00h       ; else exit
     int 21h

main endp
end main



;------------------------------------------------------------------------------;
; Programmer : Hani AL Banai                                                   ;
; COS 230                                                                      ;
; Final Exam program number 5 using 32-bit registers                           ;
; Write a program that accepts two integers from the keyboard and display their;
; greatest common divisor on standard output, using a recursive assembler      ;
; function to calculate GCD.The program should continue accepting pairs of     ;
; integers until a zero is entered for one of the integers                     ;
;------------------------------------------------------------------------------;
.model small
.386

.data
Prompt1 db 0dh,0ah,0dh,0ah
        db " Enter two integers to find their greatest common divisor (0 to exit)"
        db 0dh,0ah
        db " after entering each integer hit the ENTER key "
        db 0dh,0ah,0

Prompt2 db 0dh,0ah
        db " The greatest common divisor is :   ",0
        db 0dh,0ah,0

IntOne  dd ?
IntTwo  dd ?

.code
extrn Crlf:proc,Readint:proc,Writeint:proc

main proc
   mov ax,@data
   mov ds,ax

; Display the first message to ask the user to enter 2 integers
L0:
   mov  dx,offset Prompt1
   call Writestring

; Read what the user entered

   mov  edx,offset IntOne ; first integer
   call Readint
   cmp  eax,0
   je   L3                ; if it is equal to zero exit
   call Crlf
   push eax

   mov  edx,offset IntTwo ; second integer
   call Readint
   cmp  eax,0             ; if it is equal to zero exit
   je   L3
   call Crlf
   push eax

;  display the answering message
   mov  dx,offset Prompt2
   call Writestring

; do the calculations

   pop eax
   mov ebx,eax
   pop eax

   mov  edx,0
   idiv ebx ; for a signed division

   call GCD  ; call the gcd procedure

  ;  display the gcd for the two integers processed

   mov eax,ebx
   mov bx,10
   mov edx,eax
   call Writeint

  Loop L0 ; repeat until the user enters a zero

L3:
   mov ax,4c00h
   int 21h

main endp

; greates common divisor procedure
GCD proc
      cmp edx,0   ; do not divide by zero !
      je L8
      mov eax,ebx
      mov ebx,edx
      mov edx,0
      idiv ebx
      jmp GCD   ; call the function recursivily

 L8:            ; exit
   ret
GCD endp

; write string procedure
Writestring proc

    push    ds           ; set ES to DS
    pop     es
    mov     di,dx        ; let ES:DI point to the string
    call    Str_length   ; get length of string in AX
    mov     cx,ax        ; CX = number of bytes to write
    mov     ah,40h       ; write to file or device
    mov     bx,1         ; choose standard output
    int     21h          ; call DOS
    ret
Writestring endp

; string lenght procedure
Str_length proc
    push   cx
    push   di         ; save pointer to string
    mov    cx,0FFFFh  ; set CX to maximum word value
    mov    al,0       ; scan for null byte
    cld               ; direction = up
    repnz  scasb      ; compare AL to ES:[DI]
    dec    di         ; back up one position
    mov    ax,di      ; get ending pointer
    pop    di         ; retrieve starting pointer
    sub    ax,di      ; subtract start from end
    pop    cx
    ret               ; AX = string length
Str_length endp

end main



;------------------------------------------------------------------------------;
; Programmer : Hani AL Banai                                                   ;
; COS 230                                                                      ;
; Assignment 7                                                                 ;
; This is a user interactive program , the user should enter a variavle's name ;
; and the program will search for it in this computer's envornment . Messages  ;
; will display as the program runs to feedback the user .                      ;
;------------------------------------------------------------------------------;
.model small
.stack 100h

.data
Prompt1 db 0dh,0ah,0dh,0ah
        db "Enter the variabl's name you want to search for (blank to exit):-"
        db 0dh,0ah,0dh,0ah,0
Prompt2 db 0dh,0ah
        db "You entered :"
        db 0dh,0ah,0
Prompt3 db 0dh,0ah,0dh,0ah
        db "Sorry ! we couldnt find this variable. Please try again"
        db 0dh,0ah,0dh,0ah,0
Prompt4 db 0dh,0ah,0dh,0ah
        db "Yes ! we found this variable it is:"
        db 0dh,0ah,0
VarName db 50 dup(0),0dh,0ah,0

.code
extrn Crlf:proc

main proc
   mov ax,@data
   mov ds,ax

; Display the first message to ask the user to enter a name
L0:
   mov  dx,offset Prompt1
   call Writestring

; Read what the user entered

   mov  dx,offset VarName
   call Readstring
   call Crlf
   mov  al,0
   mov  di,offset VarName
   cmp  al,[di]
   je   L3

; Display the second message to let the user know that we read the input string

   mov dx,offset Prompt2
   call Writestring
   mov dx,offset VarName
   call Writestring

; Search for the variable in the envirnment

  mov si,offset VarName
  mov si,dx         ; let ES:DI point to the string
  call Str_length   ; get length of string in AX
  mov  cx,ax        ; CX = number of bytes
  mov es,es:[2ch]
  sub di,di
  sub al,al
  mov cx,7FFFh
  L99:
     repne scasb
     cmp byte ptr [di],0
   jne L99
  mov cx,di
  cmp cx,dx
  call compare
  jz  L1
  jmp L2

; If we find it , display the variable again

L1:
    mov dx,offset Prompt4
    call Writestring
    mov dx,offset VarName
    call Writestring
    Loop L0

; Else , display a message to say the variable is not found

L2:
   mov dx,offset Prompt3
   call Writestring
   Loop L0

; If the user did not enter a name exit

L3:
   mov ax,4c00h
   int 21h

main endp
; write string procedure
Writestring proc

    push    ds           ; set ES to DS
    pop     es
    mov     di,dx        ; let ES:DI point to the string
    call    Str_length   ; get length of string in AX
    mov     cx,ax        ; CX = number of bytes to write
    mov     ah,40h       ; write to file or device
    mov     bx,1         ; choose standard output
    int     21h          ; call DOS
    ret
Writestring endp
; string lenght procedure
Str_length proc
    push   cx
    push   di         ; save pointer to string
    mov    cx,0FFFFh  ; set CX to maximum word value
    mov    al,0       ; scan for null byte
    cld               ; direction = up
    repnz  scasb      ; compare AL to ES:[DI]
    dec    di         ; back up one position
    mov    ax,di      ; get ending pointer
    pop    di         ; retrieve starting pointer
    sub    ax,di      ; subtract start from end
    pop    cx
    ret               ; AX = string length
Str_length endp
; string comparison procedure
compare proc
    push  si
    push  di

L10:
    inc di
    inc si
    mov al,[si]
    mov al,[di]
    loopz L10
    pop di
    pop si
    ret
compare endp
; Readstring procedure :
Readstring proc
    push cx
    push si
    push cx
    mov si,dx
 A1:
    mov ah,1
    int 21h
    cmp al,0dh
    je A2
    mov [si],al
    inc si
    Loop A1

 A2:
    mov byte ptr [si],0
    pop ax
    sub ax,cx

    pop si
    pop cx
    ret
Readstring endp

end main

Copyrights 2021-2023 for Hani Al-Banai