www.flickr.com

Basic Addition in Assemble Language 8086

Sunday, October 16, 2011 Posted by Glenn

Since my classmates are all asking for help in arithmetic operation in assembly language x8086, here is a simple help. xD

Source Codes:

@prints macro s1 ;for printing string using macro command
mov ah, 09h
lea dx, s1
int 21h
endm

@scanf macro s2 ;for inputting a string using maro command
mov ah, 0ah
lea dx, s2
int 21h
endm

sseg segment para stack 'stack'
dw 200h
sseg ends

dseg segment para 'data'

inum1 label byte
max1 db 3 ;maxium lenght for the first string
len1 db ? ;unknown lenght of the inputted string
num1 db 3 dup(?)

inum2 label byte
max2 db 3 ;maxium lenght for the first string
len2 db ?
num2 db 3 dup(?)

sum db '0000' ;para san to?
pro1 db 4 dup(0) ;saka to?
pro2 db 4 dup(0) ;saka to?

s1 db 10,13,'EQUATION: (M+N)=?$'
S2 db 10,13,'ENTER VALUE FOR M: $'
S3 db 10,13,'ENTER VALUE FOR N: $'
s4 db 10,13,'THE SUM IS: $'

dseg ends

cseg segment para 'code'
main proc far
assume cs:cseg,ds:dseg,ss:sseg
mov ax, dseg
mov ds, ax
mov ax, 0003h
int 10h

cld ;para san to?
lea si, num1+1
lea di, num2+1
lea bx, sum+3

@prints s1
@prints s2
@scanf inum1

@prints s3
@scanf inum2

call mark ; call the PROCEDURE mark
mov ah, 4ch
int 21h
main endp

mark proc near ;PROCEDURE mark
mov cx, 2
xx:
mov ah, 0
mov al, [si]
adc al, [di]
aaa
mov [bx], al
dec si
dec di
dec bx
loop xx

mov [bx], ah
lea bx, sum+3

mov cx, 4
yy:
or byte ptr[bx], 30h
dec bx
loop yy

mov ah, 09h
lea dx, s4
int 21h

mov cx, 4
lea bx, sum
zz:
mov ah, 02
mov dl, [bx]
int 21h
inc bx
loop zz
ret ;return to main program
mark endp ;terminate of theprocedure mark


cseg ends
end main

---====BELOW IS THE EXPLANATION===----
xx: ;para san to?
mov ah, 0
mov al, [si]
adc al, [di]
aaa
mov [bx], al
dec si
dec di
dec bx
loop xx

-->basically, ang label na “xx” ay para na sa pag compute. Unang line: mov ah,0h -> para Masaya, I mean, kailangan mong gawing zero ang ah. Parang sa CX, kapag magccount ka, you usually use cl and not the ch, don’t you? So CL lang ang ggamitin mo tapos yung CH ay gawin mo laging zero para maiwasan na din ang error. IN FACT, kahit wag ka na mag mov ah,0h eh. Kahit wala ng ganyan.tapos yung next line na mov al,[si] ay ibig sabihin imove mo yung value ng si isa isa. Dalawang beses magloloop yang xx mo, kasi cx is set to 2 and the program requires you to enter 2 digits, kaya kapag inenter mo ay 1 digit lamang, mageerror. NEXT LINE AY “ADC”. Which means addition with carry. In the ADD command when we add two binary number bytes example,99h and 99h, the result will be 2, since when we convert 9 to binary and add them accordingly: 1001 1001 + 1001 1001 = 1 0011 0010. The left most bit will not be included in the result because the result’s data size is the same as the size of the register/memory address that the second value was added to. If the bytes are added, the sum will not fit in the byte size.
Usually this problem arises in algorithms involving byte by byte addition. If we add byte using ADC command it will set the carry flag whenever a bit will not fit in the byte size and set back the carry flag to zero when it performs the next addition operation along with the carried bit. Moving on, adc al,[di]. PARANG GANITO LANG YAN. Add mo yung si sa di tapos ilalagay mo sa al na register. Gets? But, its ADC not ADD.
Okay next tayo anne, ang AAA ay necessary. ASCII Adjust after addition. It checks the right most hex digit(4bits) of the AL register. Next is the mov [bx],al . READY FOR PRINTING after cx becomes zero. Decrement all the registers.

mov [bx], ah ;para san to?
lea bx, sum+3

-> Ito naman, yung unang line mov [bx],ah ay wala lang yan! You can make it mov [bx],0 or null. Since yung ah natin sa pinakataas ay nagging zero. ACTUALLY, THERE ARE SO MANY unnecessary codes here. Yang mov [bx],ah ay kaht burahin mo na iyan. Pati yung mov ah,0 sa itaas. Kaya nagkaroon ng ganitong code is to make sure na walang error, actually wala talagang error ang mgaganap. Para lang lagyan ng sum+3 yung bx for printing.


mov cx, 4 ;para san to?
yy:
or byte ptr[bx], 30h
dec bx
loop yy

mov ah, 09h
lea dx, s4
int 21h

-> itong loop ng yy ay basically for making the program ready to print. Next line, yung or byte ptr[bx],30h…in order to say the compiler about data type,
these prefixes should be used:

byte ptr - for byte.
word ptr - for word (two bytes).

for example:
byte ptr [BX] ; byte access.
or
word ptr [BX] ; word access.
assembler supports shorter prefixes as well:

b. - for byte ptr
w. - for word ptr

in certain cases the assembler can calculate the data type automatically. Kaya kapag tiningnan mo yung simulation ng codes mo sa isang emulator program, makikita mong b. ptr blab la bla ang gamit at hindi yung byte. Ngayon, ang tanong, bakit 30h kailangan? Kapag pinalitan mo ng value yang hex nay an, magiiba ang sagot. Ang value kasi ng 30h into decimal ay ZERO. The answer must initiate at zero.


Photobucket

"click the image to enlarge."

mov cx, 4 ;para san to?
lea bx, sum
zz:
mov ah, 02
mov dl, [bx]
int 21h
inc bx
loop zz
-> Ito na mismo ang pagpprint ng SUM. Bow!
-glenn xD

glenn von

Author: Glenn Posadas

20 year old 5th year Computer Engineering Student, a blogger, photographer, programmer, and an electronics hobbyist; a Christian who loves God very much..

Sharing is so Easy:
StumpleUpon DiggIt! Del.icio.us Blinklist Yahoo Furl Technorati Simpy Spurl Reddit Google Twitter FaceBook

  1. wow.. one of language I hate.. assembly! lol

    it's nice to find another computer engineer in the blogging world! nice!

  2. hi kuya aj. nice post. Sa mga post mo nakikita kong magaling kang programmer. Computer Engineer ka or wat? ^_^. 3rd year student palang ako. hehehe. san ka nag aral? I followed your site already. ^_^. Hate mo assembly? Medyo masaya siya. hehehe. Kasi naisip ko na, paano nagawa ng mga magagaling na inventor ng high level languages ang kanila kanilang mga language? eh siyempre sa assembly galing ang lahat, so nakakainspire din magassembly. :P. God bless.

    -this was my comment in your post. :P heheheheh

Post a Comment

Drop a comment. Thank you guys!