Assembly Language Visual Studio 2010 Exercise Part 2


Figure 1 is illustrates a hexagon figure with same length of side. To calculate the perimeter of the hexagon, the following formula is given.

Perimeter_hexagon1 = side1 + side2 + side3 + side4 + side5 + side6 Perimeter_hexagon2 = side1 + side2 + side3 + side4 + side5 + side6 TotalPerimeter = Perimeter_hexagon1 + Perimeter_hexagon2

Write a complete program using assembly language to calculate the perimeter of TWO different hexagons with different sizes.

In the program, you should do these steps:
 i. Get two values from keyboard (32-bit unsigned integer) and save into the variable name sideHex1 for the first hexagon and sideHex2 for the second hexagon.
ii. Calculate both of the perimeters (Example: Perimeter_hexagon1=18 à 3+3+3+3+3+3) by using LOOP instruction
. Save the first result in Perimeter_hexagon1 and the second result in Perimeter_hexagon2 (as 32-bit unsigned integer).
iii. Then, add the two perimeters and save in TotalPerimeter variable.
 iv. Display the output as shown in Figure 2.

Coding

INCLUDE Irvine32.inc
.data
str1 BYTE "Please enter a multiplicand (n) <1...9>: ",0
str2 BYTE "Please enter a multiplier (m) <1...9>: ",0
str3 BYTE "Multiplication of: ",0
str4 BYTE "The product is: ",0
str5 BYTE " x ",0
multiplicand dword ?
multiplier dword ?
product dword 0

.code main PROC 

 call clrscr  ; clear the console and locates the cursor at the upperleft corner.     

mov edx, offset str1  ; move the address of str1 to edx 

call WriteString   ; display the string "str1"

call ReadDec   ; read a 32bit unsigned number input from user
mov multiplicand, eax  ; move the input from user into "multiplicand"

 mov edx, offset str2  ; move the address of str2 to edx
 call WriteString   ; display the string "str1" 
call ReadDec   ; read a 32bit unsigned number input from user 
mov multiplier, eax  ; move the input from user into "multiplier"

 call crlf    ; add newline  mov edx, offset str3

 move the address of str3 to edx

call WriteString   ; display the string "str3"

mov eax, multiplicand  ; move the value from multiplicand to eax so it can be shown in decimal for next step

 call WriteDec   ; display the value of eax from multiplicand 

 mov edx, offset str5  ; move the address of str5 to edx

call WriteString   ; display the string "str5"

 mov eax, multiplier  ; move the value from multiplier to eax so it can be shown in decimal for next step

call WriteDec   ; display the value of eax from multiplier

 mov ecx, multiplier  ; initialize how many looping based on the value of multiplier

mov eax, product  ; mov the value of product to eax which is now initialize to "0"

 L1: 
add eax, multiplicand ; add the value of multiplicand to eax

 loop L1   ; Looping until the value of ecx deducted to "0" 

mov product, eax  ; store the eax value to product variable 
 call crlf    ; add newline

mov edx, offset str4  ; move the address of str4 to edx
call crlf    ; add newline  call WriteString   ; display the string "str4"

call WriteDec   ; display the value of eax, the product of addition from looping
call crlf    ; add newline

 exit main ENDP

END main





Exercise 2

• Write a program in assembly language to multiple two unsigned numbers using multiple addition.
 • Your program should ask the user to input the multiplicand (n) and the multiplier (m). 
• The program will do multiplication of (n x m) by adding (n+n+n+n+ …) m times,
 • As example: o 2 x 5 can be calculated as 2+2+2+2+2 (in another word, adding 2 five times)
 • Your program should store the multiplicand, multiplier and the result in these variables multiplicand, multiplier and product respectively.
 • You must use LOOP instruction to do the addition. 
 • Sample output
Coding 

Program 2(a) 

INCLUDE Irvine32.inc 
.data  str1 BYTE "Please enter a multiplicand (n) <1...9>: ",0  
str2 BYTE "Please enter a multiplier (m) <1...9>: ",0  
str3 BYTE "Multiplication of: ",0  
str4 BYTE "The product is: ",0  
str5 BYTE " x ",0  
multiplicand dword ? 
 multiplier dword ?  
product dword 0 

.code 
main PROC    
call clrscr  ; clear the console and locates the cursor at the upperleft corner.      
 mov edx, offset str1  ; move the address of str1 to edx   
call WriteString   ; display the string "str1"  
call ReadDec   ; read a 32bit unsigned number input from user  
mov multiplicand, eax  ; move the input from user into "multiplicand" 

 mov edx, offset str2  ; move the address of str2 to edx  
call WriteString   ; display the string "str1" 
 call ReadDec   ; read a 32bit unsigned number input from user  
mov multiplier, eax  ; move the input from user into "multiplier" 

 call crlf    ; add newline  
mov edx, offset str3  ; move the address of str3 to edx  
call WriteString   ; display the string "str3"  
mov eax, multiplicand  ; move the value from multiplicand to eax so it can be shown in decimal for next step 
 call WriteDec   ; display the value of eax from multiplicand    
mov edx, offset str5  ; move the address of str5 to edx  
call WriteString   ; display the string "str5" 
 mov eax, multiplier  ; move the value from multiplier to eax so it can be shown in decimal for next step  call WriteDec   ; display the value of eax from multiplier 

 mov ecx, multiplier  ; initialize how many looping based on the value of multiplier  mov eax, product  ; mov the value of product to eax which is now initialize to "0" 

 L1:   add eax, multiplicand ; add the value of multiplicand to eax   
loop L1   ; Looping until the value of ecx deducted to "0"   
mov product, eax  ; store the eax value to product variable     
call crlf    ; add newline  
mov edx, offset str4  ; move the address of str4 to edx  
call crlf    ; add newline  
call WriteString   ; display the string "str4"  
call WriteDec   ; display the value of eax, the product of addition from looping  c
all crlf    ; add newline 

 exit main ENDP 

END main



Program2(b)

INCLUDE Irvine32.inc 
.data  
str1 BYTE "Please enter a multiplicand (n) <1...9>: ",0  
str2 BYTE "Please enter a multiplier (m) <1...9>: ",0  
str3 BYTE "Multiplication of: ",0  
str4 BYTE "The product is: ",0  
str5 BYTE " x ",0  
multiplicand dword ?  
multiplier dword ?  
product dword 0 

.code main PROC    
call clrscr  ; clear the console and locates the cursor at the upperleft corner.  
mov edx, offset str1  ; move the address of str1 to edx   
call WriteString   ; display the string "str1"  
call ReadDec   ; read a 32bit unsigned number input from user 
 mov multiplicand, eax  ; move the input from user into "multiplicand" 

 mov edx, offset str2  ; move the address of str2 to edx  
call WriteString   ; display the string "str1"  call ReadDec   ; read a 32bit unsigned number input from user  mov multiplier, eax  ; move the input from user into "multiplier" 

 call crlf    ; add newline  mov edx, offset str3  ; move the address of str3 to edx  call WriteString   ; display the string "str3"  mov eax, multiplicand  ; move the value from multiplicand to eax so it can be shown in   decimal for next step  call WriteDec   ; display the value of eax from multiplicand    mov edx, offset str5  ; move the address of str5 to edx  call WriteString   ; display the string "str5"  mov eax, multiplier  ; move the value from multiplier to eax so it can be shown in decimal for next step 
 call WriteDec   ; display the value of eax from multiplier 

 mov ebx, multiplicand  ; move the value from multiplicand to ebx   
mul ebx  ; multiply the value from ebx "multiplicand" & eax "multiplier", 
ebx = ebx * eax     mov product, ebx  ; store the eax value to product variable 

 call crlf    ; add newline 
 mov edx, offset str4  ; move the address of str4 to edx  
call crlf    ; add newline  
call WriteString   ; display the string "str4"  
call WriteDec   ; display the value of ebx, the product of multiplication  
call crlf    ; add newline 

 exit main ENDP 

END main  

Program 3

 Coding'

INCLUDE Irvine32.inc 
.data 
str1 byte "Enter Integer : ",0  ; declare the string variable 
str2 byte "TotalODD is : ",0
 str3 byte "TotalEVEN is : ",0 
salam dword 6 dup (0)   ; declare salam array with 6 values with all initialize 0 
total1 dword ?    ; declare the totalODD variable 
total2 dword ?    ; declare the totalEVEN variable 
.code 
main PROC  
mov edx, offset str1   ; move the address of str1 to edx  
call writestring   ; to display variable str1  
call readint   ; to input data from keyboard for salam  
mov salam, eax   ; to save the input data into variable salam  
call crlf    ; add newline 

 mov edx, offset str1  ; move the address of str1 to edx  
call writestring   ; to display variable str1  
call readint   ; to input data from keyboard for [salam+4]  
mov salam+4, eax  ; to save the input data into variable [salam+4]  
call crlf    ; add newline 

 mov edx, offset str1  ; move the address of str1 to edx  
call writestring   ; to display variable str1  
call readint   ; to input data from keyboard for [salam+8]  
mov salam+8, eax  ; to save the input data into variable [salam+8]  
call crlf    ; add newline 


 mov edx, offset str1  ; move the address of str1 to edx  
call writestring   ; to display variable str1 
 call readint   ; to input data from keyboard for [salam+12] 
 mov salam+12, eax  ; to save the input data into variable [salam+12]  
call crlf    ; add newline 

 mov edx, offset str1  ; move the address of str1 to edx  
call writestring   ; to display variable str1  
call readint   ; to input data from keyboard for [salam+16] 
mov salam+16, eax  ; to save the input data into variable [salam+16]  
call crlf    ; add newline 

 mov edx, offset str1  ; move the address of str1 to edx  
call writestring   ; to display variable str1  
call readint   ; to input data from keyboard for [salam+20] 
 mov [salam+20], eax  ; to save the input data into variable [salam+20]  
call crlf    ; add newline 

 mov edi, offset salam  ; move the address of salam to edi  
mov ecx, 3   ; loop counter  
mov eax, 0   ; move 0 value to eax to clear eax
 L1:  
add eax, [edi]   ; add the value of edi into eax register  
add edi, 8   ; add the edi register address by 8  
loop L1    ; loop to L1 

 mov total1, eax   ; move the eax resigter value into variable totalODD     
mov edi, offset [salam + 4] ; move the address of [salam+4] to edi  
mov ecx, 3   ; loop counter  
mov eax, 0   ; move 0 value to eax to clear eax 

L2:  
add eax, [edi]   ; add the value of edi into eax register  
add edi, 8   ; add the edi register address by 8  
loop L2    ; loop to L2 

 mov total2, eax   ; move the eax resigter value into variable totalEVEN  
  mov edx, offset str2  ; move the address of str2 to edx  
call writestring   ; to display variable str2
 mov eax, total1   ; move value of totalOOD to eax  
call writedec   ; display the eax register value  
call crlf    ; add newline      
mov edx, offset str3  ; move the address of str3 to edx 
 call writestring   ; to display variable str3  
mov eax, total2   ; move value of totalEVEN to eax  
call writedec   ; display the eax register value 
 call crlf    ; display the eax register value  ;   
exit main ENDP 

END main 

Post a Comment

4 Comments

  1. bisa di perjelas gk gambarnya :"(

    ReplyDelete
  2. This comment has been removed by a blog administrator.

    ReplyDelete
  3. bro may god bless you for this i totally got no idea at all about how to run it

    ReplyDelete
  4. hello bro, for question 3 you didn't show the GrandTotal coding. can you show how to do it ?

    ReplyDelete