You are on page 1of 4

Exercise 01 As

Chapter Assembly (2 point)


Ex1:.
ADD: will store the remainder to destination
Adds "src" to "dest" and replacing the original contents of "dest".
Both operands are binary.

SUB: will store the remainder to destination


The source is subtracted from the destination and the result is
stored in the destination.

MUL: Unsigned multiply of the accumulator by the source.  If "src" is
a byte value, then AL is used as the other multiplicand and the
result is placed in AX.  If "src" is a word value, then AX is
multiplied by "src" and DX:AX receives the result.  If "src" is
a double word value, then EAX is multiplied by "src" and EDX:EAX
receives the result.  The 386+ uses an early out algorithm which
makes multiplying any size value in EAX as fast as in the 8 or 16
bit registers.

IMUL: Signed multiplication of accumulator by "src" with result placed
in the accumulator.  If the source operand is a byte value, it
is multiplied by AL and the result stored in AX.  If the source
operand is a word value it is multiplied by AX and the result is
stored in DX:AX.  Other variations of this instruction allow
specification of source and destination registers as well as a
third immediate factor.

DIV: Unsigned binary division of accumulator by source.  If the source
divisor is a byte value then AX is divided by "src" and the quotient
is placed in AL and the remainder in AH.  If source operand is a word
value, then DX:AX is divided by "src" and the quotient is stored in AX
and the remainder in DX.

IDIV: Signed binary division of accumulator by source.  If source is a
byte value, AX is divided by "src" and the quotient is stored in
AL and the remainder in AH.  If source is a word value, DX:AX is
divided by "src", and the quotient is stored in AL and the
remainder in DX.

Problem (8 point)
Ex2:
; «« Comment begins with ';' to the end of a line
; From masm32\tutorial\console\demo1
;
; Build this with the "Project" menu using
; "Console Assemble and Link"
; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««

include \masm32\include\masm32rt.inc

.code ; Tell MASM where the code starts

start: ; The CODE entry point to the program


print chr$("Han Mac Tu",13,10) ; 13: carriage return, 10: new line
print chr$("Song co xanh tuoi gon toi troi",13,10) ; 13: carriage return, 10: new line
print chr$("bao co thon nu hat tren doi",13,10) ; 13: carriage return, 10: new line
print chr$("Ngay mai trong dam xuan xanh ay",13,10) ; 13: carriage return, 10: new line
print chr$("Co ke theo chong bo cuoc choi",13,10) ; 13: carriage return, 10: new line

exit ; exit the program

; -------------------------------
end start ; Tell MASM where the program ends

Ex3:
include \masm32\include\masm32rt.inc
.data
var1 dd 0
var2 dd 0
var3 dd 0
sum dd 0
avg dd 0.00
.code
start:
mov var1, sval(input("Enter number 1: "))
mov var2, sval(input("Enter number 2: "))
mov var3, sval(input("Enter number 3: "))
mov eax,var2 ; eax = var2
mov ecx,var1 ; ecx = var1
add ecx,eax ; ecx = ecx + eax
mov eax,ecx ; eax = ecx
add eax,var3 ; eax = eax + var3
mov sum,eax ; sum = eax
print chr$("Sum of 3 numbers: ")
print str$(sum) ;print out sum
print chr$(" ",13,10)
mov edx,0 ; edx = 0 (clear data)
mov eax,sum ; eax = sum
cdq ; convert number in eax
mov ecx,3 ; ecx = 3
div ecx ; eax = eax/3 (edx = eax%3)
mov avg,eax ; avg = eax
print chr$("Average of 3 numbers: ")
print str$(avg) ; print out average
print chr$(" ",13,10)
exit
end start
Ex4:
include \masm32\include\masm32rt.inc
.code
start:
call main
exit
main proc
; Solve ax+b=0
local a:DWORD
local b:DWORD
local x:REAL4
print chr$("Solve equation ax+b=0",13,10)
mov a, sval(input("Enter a: ")) ; Get input from user
mov b, sval(input("Enter b: ")) ;
mov eax, b ; eax = b
neg eax ; change signed of b (b = -b)
cdq ; convert eax
idiv a ; eax = eax/a
;mov x,eax
print str$(eax)
ret
main endp
end start
Ex5:

include \masm32\include\masm32rt.inc
.code
start:
call main
exit
main proc
LOCAL var1:DWORD
LOCAL var2:DWORD
LOCAL sum:DWORD
LOCAL diff:DWORD
LOCAL prod:DWORD
LOCAL quot:DWORD
mov var1, sval(input("Input number 1: ")) ; get input from user
mov var2, sval(input("Input number 2: "))
;v1+v2
mov eax,var1 ; eax = var1
mov ecx,var2 ; ecx = var2
add eax,ecx ; eax = eax + ecx
mov sum,eax ; sum = eax
print chr$("Sum of 2 numbers: ")
print str$(sum) ; print out sum
print chr$(" ",13,10)
;v1-v2
mov eax,var1 ; eax = var1
mov ecx,var2 ; ecx = var2
sub eax,ecx ; eax = eax - ecx
mov diff,eax ; diff = eax
print chr$("Difference of 2 numbers: ")
print str$(diff) ; print out diff
print chr$(" ",13,10)
;v1*v2
mov eax,var1 ; eax = var1
mov ecx,var2 ; ecx = var2
imul eax,ecx ; eax = eax*ecx
mov prod,eax ; prod = eax
print chr$("Product of 2 numbers: ")
print str$(prod) ; print out prod
print chr$(" ",13,10)
;v1/v2
mov eax,var1 ; eax = var1
cdq ; convert eax
mov ecx,var2 ; ecx = var2
cmp ecx, 0
je IS_ZERO
jne NOT_ZERO
V2 # 0:
div ecx ; eax = eax/ecx
mov quot,eax ; quot = eax
print chr$("Quotient of 2 numbers: ")
print str$(quot) ; print out quot
print chr$(" ",13,10)
ret
V2 = 0:
print chr$("Because V2 = 0, so, V2 is invalid!!!")
ret
ret
main endp
end start

You might also like