banner



How To Subtract The Values From Two Registers And Store The Answer In Another Register

Assembly - Arithmetic Instructions



The INC Teaching

The INC instruction is used for incrementing an operand by 1. Information technology works on a unmarried operand that can exist either in a register or in memory.

Syntax

The INC instruction has the following syntax −

INC destination        

The operand destination could be an 8-bit, 16-bit or 32-flake operand.

Example

INC EBX	     ; Increments 32-bit register INC DL       ; Increments 8-bit register INC [count]  ; Increments the count variable        

The DEC Teaching

The Dec instruction is used for decrementing an operand by i. Information technology works on a unmarried operand that tin be either in a register or in retentivity.

Syntax

The DEC instruction has the post-obit syntax −

Dec destination        

The operand destination could be an 8-flake, sixteen-scrap or 32-scrap operand.

Example

segment .data    count dw  0    value db  xv 	 segment .text    inc [count]    dec [value] 	    mov ebx, count    inc word [ebx] 	    mov esi, value    december byte [esi]        

The ADD and SUB Instructions

The ADD and SUB instructions are used for performing elementary addition/subtraction of binary data in byte, word and doubleword size, i.e., for adding or subtracting 8-bit, xvi-bit or 32-bit operands, respectively.

Syntax

The ADD and SUB instructions take the following syntax −

Add together/SUB	destination, source        

The ADD/SUB instruction can have identify betwixt −

  • Register to annals
  • Memory to annals
  • Register to memory
  • Register to constant data
  • Memory to constant data

However, like other instructions, memory-to-retentivity operations are not possible using Add together/SUB instructions. An ADD or SUB functioning sets or clears the overflow and deport flags.

Example

The following example will ask two digits from the user, store the digits in the EAX and EBX register, respectively, add the values, store the result in a memory location 'res' and finally brandish the result.

SYS_EXIT  equ 1 SYS_READ  equ three SYS_WRITE equ four STDIN     equ 0 STDOUT    equ 1  segment .data      msg1 db "Enter a digit ", 0xA,0xD     len1 equ $- msg1      msg2 db "Please enter a 2d digit", 0xA,0xD     len2 equ $- msg2      msg3 db "The sum is: "    len3 equ $- msg3  segment .bss     num1 resb 2     num2 resb ii     res resb 1      department	.text    global _start    ;must be alleged for using gcc 	 _start:             ;tell linker entry point    mov eax, SYS_WRITE             mov ebx, STDOUT             mov ecx, msg1             mov edx, len1     int 0x80                     mov eax, SYS_READ     mov ebx, STDIN      mov ecx, num1     mov edx, 2    int 0x80                 mov eax, SYS_WRITE            mov ebx, STDOUT             mov ecx, msg2              mov edx, len2             int 0x80     mov eax, SYS_READ      mov ebx, STDIN      mov ecx, num2     mov edx, 2    int 0x80             mov eax, SYS_WRITE             mov ebx, STDOUT             mov ecx, msg3              mov edx, len3             int 0x80     ; moving the first number to eax register and second number to ebx    ; and subtracting ascii '0' to catechumen it into a decimal number 	    mov eax, [num1]    sub eax, '0' 	    mov ebx, [num2]    sub ebx, '0'     ; add eax and ebx    add eax, ebx    ; add '0' to to convert the sum from decimal to ASCII    add together eax, '0'     ; storing the sum in memory location res    mov [res], eax     ; print the sum     mov eax, SYS_WRITE            mov ebx, STDOUT    mov ecx, res             mov edx, i            int 0x80  get out:            mov eax, SYS_EXIT       xor ebx, ebx     int 0x80        

When the higher up code is compiled and executed, it produces the following result −

Enter a digit: 3 Please enter a second digit: 4 The sum is: vii        

The program with hardcoded variables −

section	.text    global _start    ;must be alleged for using gcc 	 _start:             ;tell linker entry point    mov	eax,'3'    sub     eax, '0' 	    mov 	ebx, '4'    sub     ebx, '0'    add together 	eax, ebx    add together	eax, '0' 	    mov 	[sum], eax    mov	ecx,msg	    mov	edx, len    mov	ebx,ane	;file descriptor (stdout)    mov	eax,4	;arrangement telephone call number (sys_write)    int	0x80	;call kernel 	    mov	ecx,sum    mov	edx, one    mov	ebx,ane	;file descriptor (stdout)    mov	eax,four	;organization phone call number (sys_write)    int	0x80	;phone call kernel 	    mov	eax,ane	;system telephone call number (sys_exit)    int	0x80	;call kernel 	 section .data    msg db "The sum is:", 0xA,0xD     len equ $ - msg       segment .bss    sum resb 1        

When the to a higher place code is compiled and executed, it produces the following result −

The sum is: vii        

The MUL/IMUL Pedagogy

In that location are two instructions for multiplying binary information. The MUL (Multiply) instruction handles unsigned data and the IMUL (Integer Multiply) handles signed data. Both instructions affect the Carry and Overflow flag.

Syntax

The syntax for the MUL/IMUL instructions is as follows −

MUL/IMUL multiplier        

Multiplicand in both cases will be in an accumulator, depending upon the size of the multiplicand and the multiplier and the generated product is as well stored in two registers depending upon the size of the operands. Following section explains MUL instructions with iii different cases −

Sr.No. Scenarios
i

When two bytes are multiplied −

The multiplicand is in the AL annals, and the multiplier is a byte in the retentivity or in another register. The product is in AX. High-society viii bits of the production is stored in AH and the low-order 8 $.25 are stored in AL.

Arithmetic1

2

When two one-word values are multiplied −

The multiplicand should be in the AX register, and the multiplier is a word in retentiveness or another annals. For case, for an teaching similar MUL DX, you must store the multiplier in DX and the multiplicand in AX.

The resultant product is a doubleword, which will need two registers. The high-order (leftmost) portion gets stored in DX and the lower-gild (rightmost) portion gets stored in AX.

Arithmetic2

3

When 2 doubleword values are multiplied −

When two doubleword values are multiplied, the multiplicand should be in EAX and the multiplier is a doubleword value stored in memory or in some other register. The product generated is stored in the EDX:EAX registers, i.eastward., the high gild 32 bits gets stored in the EDX register and the low social club 32-bits are stored in the EAX annals.

Arithmetic3

Example

MOV AL, 10 MOV DL, 25 MUL DL ... MOV DL, 0FFH	; DL= -1 MOV AL, 0BEH	; AL = -66 IMUL DL        

Example

The post-obit example multiplies 3 with 2, and displays the result −

section	.text    global _start    ;must be declared for using gcc 	 _start:             ;tell linker entry point     mov	al,'3'    sub     al, '0' 	    mov 	bl, '2'    sub     bl, '0'    mul 	bl    add together	al, '0' 	    mov 	[res], al    mov	ecx,msg	    mov	edx, len    mov	ebx,1	;file descriptor (stdout)    mov	eax,four	;system phone call number (sys_write)    int	0x80	;call kernel 	    mov	ecx,res    mov	edx, 1    mov	ebx,1	;file descriptor (stdout)    mov	eax,4	;system call number (sys_write)    int	0x80	;call kernel 	    mov	eax,ane	;system call number (sys_exit)    int	0x80	;call kernel  department .data msg db "The result is:", 0xA,0xD  len equ $- msg    segment .bss res resb 1        

When the in a higher place code is compiled and executed, information technology produces the post-obit issue −

The event is: 6        

The DIV/IDIV Instructions

The partition operation generates two elements - a quotient and a residuum. In example of multiplication, overflow does not occur considering double-length registers are used to go on the product. However, in case of division, overflow may occur. The processor generates an interrupt if overflow occurs.

The DIV (Dissever) instruction is used for unsigned data and the IDIV (Integer Divide) is used for signed data.

Syntax

The format for the DIV/IDIV instruction −

DIV/IDIV	divisor        

The dividend is in an accumulator. Both the instructions can work with 8-bit, xvi-flake or 32-fleck operands. The performance affects all half dozen status flags. Following section explains iii cases of division with dissimilar operand size −

Sr.No. Scenarios
1

When the divisor is 1 byte −

The dividend is causeless to be in the AX register (sixteen bits). Afterward segmentation, the quotient goes to the AL register and the balance goes to the AH register.

Arithmetic4

ii

When the divisor is i word −

The dividend is assumed to be 32 bits long and in the DX:AX registers. The high-gild xvi bits are in DX and the low-order 16 bits are in AX. After division, the 16-bit quotient goes to the AX annals and the sixteen-scrap remainder goes to the DX register.

Arithmetic5

3

When the divisor is doubleword −

The dividend is assumed to exist 64 bits long and in the EDX:EAX registers. The high-social club 32 bits are in EDX and the low-order 32 bits are in EAX. After segmentation, the 32-bit quotient goes to the EAX register and the 32-bit residuum goes to the EDX annals.

Arithmetic6

Example

The post-obit case divides viii with 2. The dividend 8 is stored in the 16-bit AX annals and the divisor 2 is stored in the viii-scrap BL register.

section	.text    global _start    ;must be declared for using gcc 	 _start:             ;tell linker entry point    mov	ax,'8'    sub     ax, '0' 	    mov 	bl, '2'    sub     bl, '0'    div 	bl    add	ax, '0' 	    mov 	[res], ax    mov	ecx,msg	    mov	edx, len    mov	ebx,one	;file descriptor (stdout)    mov	eax,4	;system phone call number (sys_write)    int	0x80	;phone call kernel 	    mov	ecx,res    mov	edx, i    mov	ebx,1	;file descriptor (stdout)    mov	eax,four	;organisation call number (sys_write)    int	0x80	;telephone call kernel 	    mov	eax,1	;system call number (sys_exit)    int	0x80	;call kernel 	 section .information msg db "The outcome is:", 0xA,0xD  len equ $- msg    segment .bss res resb i        

When the above code is compiled and executed, it produces the following effect −

The result is: iv        

How To Subtract The Values From Two Registers And Store The Answer In Another Register,

Source: https://www.tutorialspoint.com/assembly_programming/assembly_arithmetic_instructions.htm

Posted by: hunterlasuall.blogspot.com

0 Response to "How To Subtract The Values From Two Registers And Store The Answer In Another Register"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel