Assembly Code Snippet
  • Assembly Code Snippet
  • Assembly Program: 4-Digit Decimal Number Addition
  • ASCII Division and Display in x86 Assembly
  • ASCII Arithmetic Addition in x86 Assembly
  • BCD Addition and Display in Assembly
  • BCD Arithmetic and Display in Assembly
  • Decimal Multiplication and Display in x86 Assembly
  • Assembly Program: Determining Endianness and Displaying Messages
  • Assembly Program: Determining Even and Odd Numbers
  • Assembly Program: Displaying Messages using Syscalls
  • Assembly Program: Finding and Printing the Largest Word in a Sentence
  • Assembly Program: Finding the Largest of Three Numbers
  • Displaying "Hello World!" in x86 Assembly
  • Increment and Display Digits in Assembly
  • Input and Display User Name in Linux Assembly
  • Input and Display User Number in Linux Assembly
  • Introduction to Linux Assembly Programming with Messages
  • Assembly Program: Demonstrating Loop and Testing
  • Changing and Displaying Names in Assembly
  • Selection Sort from Minimum to Maximum
  • Assembly Program: Sentence Word Deletion
  • Sum and Read Input Numbers:
  • Two-Digit Sum and Square - Assembly Program
  • Calculate and Display Sum
  • Sorting Numerical Elements (BubbleSort)
    • Assembly Program: Sorting Numerical Elements
Powered by GitBook
On this page
  • source code
  • How It Works
  • Example
  • How to Run

Was this helpful?

Edit on GitHub

ASCII Division and Display in x86 Assembly

This x86 assembly code showcases a simple division operation on ASCII-encoded digits. The program takes two ASCII digits as input, converts them to decimal equivalents, performs division, and then displays the result.

source code

section	.text
   global _start    ; Entry point for the program
	
_start:             ; Start of the program
   mov	ax,'8'      ; Load ASCII value of '8' into AX
   sub     ax, '0'   ; Convert ASCII to decimal
	
   mov 	bl, '2'    ; Load ASCII value of '2' into BL
   sub     bl, '0'   ; Convert ASCII to decimal
	
   div 	bl          ; Divide AX by BL, quotient in AL, remainder in AH
   add	ax, '0'     ; Convert result to ASCII
	
   mov 	[res], ax  ; Store the ASCII result in memory location 'res'
   mov	ecx,msg	    ; Load address of message into ECX
   mov	edx, len    ; Load length of message into EDX
   mov	ebx,1	    ; File descriptor (stdout)
   mov	eax,4	    ; System call number (sys_write)
   int	0x80	    ; Call kernel to display message
	
   mov	ecx,res     ; Load address of ASCII result into ECX
   mov	edx, 1      ; Set length to 1 (single character)
   mov	ebx,1	    ; File descriptor (stdout)
   mov	eax,4	    ; System call number (sys_write)
   int	0x80	    ; Call kernel to display result
	
   mov	eax,1	    ; System call number (sys_exit)
   int	0x80	    ; Call kernel to exit program
	
section .data
msg db "The result is:", 0xA,0xD ; Message to display
len equ $- msg   ; Calculate length of message
segment .bss
res resb 1         ; Storage for the calculated result

How It Works

  1. The program begins by loading the ASCII values of two digits into registers.

  2. It converts the ASCII values to their corresponding decimal values by subtracting the ASCII value of '0'.

  3. Division is performed on the decimal values by using the div instruction. The dividend is stored in AX, and the divisor is loaded into BL. The quotient will be in AL, and the remainder in AH.

  4. The decimal quotient is then converted back to its ASCII representation by adding the ASCII value of '0'.

  5. The result is displayed along with an appropriate message.

Example

For example, let's consider the input '8' and '2':

  1. The ASCII value of '8' (56) is converted to the decimal value 8.

  2. The ASCII value of '2' (50) is converted to the decimal value 2.

  3. Division of 8 by 2 results in a quotient of 4.

  4. The decimal quotient 4 is then converted back to its ASCII representation (52).

  5. The program will display: "The result is: 4".

How to Run

To run this program:

  1. Assemble the code using an assembler like NASM:

    nasm -f elf file.asm -o file.o
  2. Link the object file to create an executable:

    ld -m elf_i386 file.o -o file
  3. Run the executable:

    ./file

Ensure you have NASM and a compatible x86 assembler installed.

PreviousAssembly Program: 4-Digit Decimal Number AdditionNextASCII Arithmetic Addition in x86 Assembly

Last updated 1 year ago

Was this helpful?