Assembly - number ASCII and bsd

Prahlad Godara ------ From DOOSEEP

Content

Assembly language numerical data and arithmetic instructions operate on binary data. When numbers are displayed on a screen or entered from a keyboard, they are in ASCII form. We have to convert from binary to ASCII or decimal number to understand the data.

numbers in assembly language

Assembly language programming allows processing numbers in binary form in a more efficient way. Decimal numbers can be represented in two forms -

  1. ASCII form
  2. BCD or Binary Coded Decimal form

ASCII Representation

In ASCII representation, a decimal number is stored as a string of ASCII characters. For example, the decimal value 1234 is stored as -.

 31	32	33	34H 49H 

Where, 31H is the ASCII value for 1, 32H is the ASCII value for 2, 39H is the ASCII value for 9, and so on.

There are four instructions to process a number in ASCII representation -
  1. AAA − ASCII Adjust After Addition
  2. AAS − ASCII Adjust After Subtraction
  3. AAM − ASCII Adjust After Multiplication
  4. AAD − ASCII Adjust Before Division
These instructions take no operands and assume the required operands in the AL register.

BCD Representation

There are two types of BCD representation -
  1. Unpacked BCD
  2. Pack BCD

In the unpacked BCD representation, each byte stores the binary equivalent of a decimal digit. For example, the number 1234 is stored as -

01	02	03	04H

There are two instructions to process these numbers -

  1. AAM - Adjust After ASCII Multiplication
  2. AAD - Adjust before ASCII division

The four ASCII accommodated instructions, AAA, AAS, AAM and AAD, can also be used with the unpacked BCD representation. In packed BCD representation, each digit is stored using four bits. Two decimal digits are packed into one byte. For example, the number 1234 is stored as -

12	34H
There are two instructions to process these numbers -
  1. DAA − Decimal Adjustment After Addition
  2. DAS - Decimal Adjustment After Subtraction
  3. Packed BCD representation has no support for multiplication and division.

Example

The following program adds two 5-digit decimal numbers and displays the sum. It uses the above concepts -


  section	.text
  global _start        ;must be declared for using gcc

_start:	                ;tell linker entry point

  mov     esi, 4       ;pointing to the rightmost digit
  mov     ecx, 5       ;num of digits
  clc
add_loop:  
  mov 	al, [num1 + esi]
  adc 	al, [num2 + esi]
  aaa
  pushf
  or 	al, 30h
  popf
 
  mov	[sum + esi], al
  dec	esi
  loop	add_loop
 
  mov	edx,len	        ;message length
  mov	ecx,msg	        ;message to write
  mov	ebx,1	        ;file descriptor (stdout)
  mov	eax,4	        ;system call number (sys_write)
  int	0x80	        ;call kernel
 
  mov	edx,5	        ;message length
  mov	ecx,sum	        ;message to write
  mov	ebx,1	        ;file descriptor (stdout)
  mov	eax,4	        ;system call number (sys_write)
  int	0x80	        ;call kernel
 
  mov	eax,1	        ;system call number (sys_exit)
  int	0x80	        ;call kernel

section	.data
msg db 'The Sum is:',0xa	
len equ $ - msg			
num1 db '12345'
num2 db '23456'
sum db '  

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

 The Sum is: 35801


Tags- This blogcreates content similar to stackoverflow geeks for geeks tutorialspoint w3schools and dooseep