Showing posts with label Assembly Language. Show all posts
Showing posts with label Assembly Language. Show all posts

Assembly Language

Sample Program

name "calc-sum"

org 100h ; directive make tiny com file.

; calculate the sum of elements in vector,
; store result in m and print it in binary code.

; number of elements:
mov cx, 5

; al will store the sum:
mov al, 0

; bx is an index:
mov bx, 0

; sum elements:
next: add al, vector[bx]

; next byte:
inc bx

; loop until cx=0:
loop next


; store result in m:
mov m, al


; print result in binary:
mov bl, m
mov cx, 8
print: mov ah, 2 ; print function.
mov dl, '0'
test bl, 10000000b ; test first bit.
jz zero
mov dl, '1'
zero: int 21h
shl bl, 1
loop print
; print binary suffix:
mov dl, 'b'
int 21h



mov dl, 0ah ; new line.
int 21h
mov dl, 0dh ; carrige return.
int 21h


; print result in decimal:
mov al, m
call print_al




; wait for any key press:
mov ah, 0
int 16h



ret

; variables:
vector db 5, 4, 5, 2, 1
m db 0


print_al proc
cmp al, 0
jne print_al_r
push ax
mov al, '0'
mov ah, 0eh
int 10h
pop ax
ret
print_al_r:
pusha
mov ah, 0
cmp ax, 0
je pn_done
mov dl, 10
div dl
call print_al_r
mov al, ah
add al, 30h
mov ah, 0eh
int 10h
jmp pn_done
pn_done:
popa
ret
endp




The Purpose / Use Of Assembly Language

  • The first reason to work with assembler is that it provides the opportunity of knowing more the operation of your PC, which allows the development of software in a more consistent manner.
  • The second reason is the total control of the PC which you can have with the use of the assembler.
  • Another reason is that the assembly programs are quicker, smaller, and have larger capacities than ones created with other languages.
  • Lastly, the assembler allows an ideal optimization in programs, be it on their size or on their execution.

What is Assembly language?

Assembly language is the most basic programming language available for any processor. With assembly language, a programmer works only with operations implemented directly on the physical CPU. Assembly language lacks high-level conveniences such as variables and functions, and it is not portable between various families of processors. Nevertheless, assembly language is the most powerful computer programming language available, and it gives programmers the insight required to write effective code in high-level languages. Learning assembly language is well worth the time and effort of every serious programmer.