Skip to main content

Convert Integer Into ASCII Assembly language Example

Hi Friends,



Welcome Again Friends, . I hope You all are Enjoying Series of Assembly Tutorials Well, Actually Assembly Is Very Important Part To Keep Strong Grep Over Exploit writing. that's why I am trying To Polish my Assembly Programming Writing Skill. So, Today Again I am going To share Another piece of Code Of Assembly language Where I tried To Demonstrate How To Convert Digits Into ASCII codes To Print Over the Terminal. I know, many of you are now confused. why?

Introduction


Actually, In Assembly, Printing Things Over the Terminal is Not that straight forward. When we print number in other high level programming language then, that programming language automatically handles all internal process. so, that user don't need to mess with low level process. but in assembly you have to done all on your own. So, basically In  assembly you actually can print only according to ASCII Table. Basically, All Your Hard Coded String, Character Automatically Gets Converted Into ASCII Table During Compilation By Compilers. But Note. Its Only Hard Coded Codes, Hence If Any Digit Gets Generated During Any Process Then You Have To Write Your Own Sub Routine To Convert it Into ASCII Code. I hope Now You All Got it. So, Let's Try It now.

How?

Read Comments Carefully!


By The Way, If you Are new And Don't Know How To Start, Compile Or Run?
Then, Don't Follow up Our Previous Blog post. Here, it's The List.

1. Hello World In Assembly
2. Find String Length In Assembly Language
3. Subroutine Demo In Assembly language
4. Some Subroutine Functionality Example
5. User Input Example In Assembly Language
6. Variable Decleration/Include function In Assembly


Convert Integer To ASCII Table In Assembly Language

Digit Printing Script (DigitPrinting.asm)

;    Digit printing concept
; author : Suraj Singh Bisht
; Email   : surajsinghbisht054@gmail.com
;
; includable functions
%include 'includable_functions_without_start.asm'
;
;   I Think This Script needs Little Finishing. 
;
; So, i will left this work to reader :))
;
;
; ====================================================
;              [ Includable File]
; Functions:
;       _strlen
;       _exit
;       _print
;       _printline
;
; ====================================================
;
;
; Simple Concept to print multi digit numbers
;
; For Example :
; Dividend  = 1000 (Dividend)
; Divisor   = 10
;
; ( Quotient, Remainder ) = ( Dividend / Divisor )
;
;
; While (if Quotient is Not Equal To 0){
; print (Remainder);
;
; ( Quotient, Remainder ) = ( Quotient / Divisor )
;
; }
;
;
;
; Come On! Use Mind And Calculate Above Provided Algo.
;
;

__printreverse:
 ; input
 ;  eax = address of string
 ;
 ; output
 ; nothing
 push ebx
 push ecx
 push edx

 ; store value into stack
 push eax
 call _strlen  ; Find String length
 mov ecx, eax  ; ECX = Length Of String
 pop ebx   ; EBX = Address of string

_printreverseiterate:
 mov al, [ebx+ecx]  ; Load Address Into EAX
 call _printsingle  ; print single number over terminal
 cmp ecx, 0   ; Compare if ECX is zero
 je _printreversereturn  ; Return
 dec ecx
 jmp _printreverseiterate ; Repeat


_printreversereturn:
 pop edx
 pop ecx
 pop ebx
 ret


; routine to print single digit
_printsingle:
        ; Input:
        ;       EAX = Value
        ; Output:
        ;       nothing
        push ebx        ; store register into stack

        mov ebx, 0      ; EBX = 0
        push ebx        ; push ebx
        push eax        ; push eax
        mov eax, esp    ; EAX = ESP
        call _print     ; call print 

        pop eax         ; pop Eax
        pop ebx         ; pop EBX Null Value
        pop ebx         ; pop eax

        ret             ; return 


; Digit To ASCII Digit Convertion Subroutine
_printdigit:
        ; Input:
        ;       EAX = Digit Number
        ; Output
        ;       EAX
        ;

 ; Save Values
        push eax                ; Save Digit [Previous Arguments]
        push ebx                ; Push Ebx To Stack [Previous Arguments]
 push ecx
 push edx
 mov ebx, 10  ; Set EBX = 10
 mov ecx, 0  ;

; division loop
_divisionloop:
 xor edx, edx  ; EDX = 0
        div ebx                 ; Perform Division Between EAX, EBX [EDX, EAX = EAX / EBX ]
                                ; EAX = Quotient And EDX = Remainder

        push eax                ; Push EAX Into Stack [Division Answer]
        mov eax, edx            ; EAX = EDX [EAX = Remainder]

        add eax, 48             ; ASCII code Difference Is 48
 mov [digitprintersave+ecx], eax  ;
        ;call _printsingle       ; Call printsingle routine
 inc ecx;
        pop eax                 ; Get EAX
        cmp eax, 0              ; Compare
        je _comparisionsection  ; To Avoid Print
 jmp _divisionloop ; Repeat


_comparisionsection:
 mov eax, digitprintersave  ;
 call __printreverse
 ; Pop Values
 pop edx
 pop ecx
        pop ebx                 ; Pop Digit [Previous Arguments]
        pop eax                 ; Pop Digit[Previous Arguments]

        ret             ; Return





; section bss
SECTION .bss

; section data
SECTION .data

; section text
SECTION .text
 global _start


_iterateNumber:
        ; print digit routine
        call _printdigit        ; call _printdigit

        ; increase number
        inc     eax             ; increase EAX = EAX + 1

 ; Compare
 cmp eax, 100  ; Compare EAX==0, if Yes, Set Zero Flag = 1 Else Zero Flag = 0


        ; Check, Zero Flag
        jne _iterateNumber       ; Jump if not Equal _iterateNumber

 ; Exit Routine
 jmp _exit


_start:
        mov eax, 0      ; EAX = 0
 jmp _iterateNumber ;

Includable Function (includable_functions_without_start.asm)

; Assembly Includable File
; author : suraj singh bisht
; Email  : surajsinghbisht054@gmail.com
;
; ====================================================
;              [ Includable File]
; Functions:
; _strlen
; _exit
; _print
; _printline
;
; ====================================================





; Routine To Find String length
_strlen:
 ; Input
 ; EAX = address of string
 ; Output
 ; EAX = Length of String

 ; Initialize Registers
 push ebx ; push ebx value into stack
 mov ebx, 0 ; Set EBX = 0


; iteration Routine
_iterate:
 cmp byte [eax], 0 ; Compare Last bit, if true, Set Zero flag = 1 Else Zero flag = 0
 jz _return  ; If Zero Flag = 1, Then Call function else pass
 inc eax   ; Move Memory Address Pointer 
 inc ebx   ; Keep Record of Iteration
 jmp _iterate  ; Repeat

; Return String Calculated Length
_return:
 mov eax, ebx ; Set Return Value EAX = EBX
 pop ebx  ; POP EBX Value
 ret  ; Return

; Exit Routine
_exit:
 mov eax, 1  ; Invoke SYS_EXIT
 mov ebx, 0 ; set Return Value Zero
 int 80h  ; interpt kernel


; Print Line
_print:
 ; Input 
 ; EAX = addressof(string)
 ; Output
 ; Nothing

 push ebx  ; Store Registers Into Stack
 push ecx
 push edx
 push eax

 call _strlen ; Find String Len
 mov edx, eax ; Set EDX= EAX {String Length}
 mov eax, 4 ; invoke SYS_WRITE
 mov ebx, 1 ; STDOUT
 pop ecx  ; POP String Address
 int 80h  ; interpt Kernel
 pop edx  ; POP all registers previous values
 pop ecx  ;
 pop ebx  ;
 ret  ; return

; PrintLine 
_printline:
 ; Input
 ; EAX = addressof(string)
 ; Output
 ;  nothing
 
 call _print ; Print String 

 ; 
 mov eax, 0 ; SET EAX = 0
 push eax ; PUSH EAX Into Stack To make Sure; String End Character is 0

 mov eax, 0AH ; New Line ASCII Code
 push eax ; Push into The Stack

;
;  Now Our Stack Look Like This
;     ->     ->        ->       ->        ->
;  ---------------------------------------------------
;   |   EAX[0AH]   |   EAX [0]   | previous data |
;  ---------------------------------------------------
;  ESP                                             EBP
;

 mov eax, esp ; Set EAX = ESP
 call _print ; call _print routine
 pop eax  ; Remove not useful pushed values from stack
 pop eax  ;
 ret  ; Return



Please Check Github For Latest Error Free Examples Codes. Click here

Related Post

Top Visited

how to install burp suite in Linux/Ubuntu 16.04

Create Simple Packet Sniffer Using Python

Big List Of Google Dorks For Sqli Injection

Latest Google Dorks List

how to configure burpsuite with firefox?

List of Keyboard Shortcuts Keys for GNOME Desktop (Kali linux / Linux / Ubuntu/*nix )

Python Beautiful Soup Module - Tutorial - Part 2

Create Ping Sweeping Script Using Python

How to create Phishing Page Using Kali Linux | Webpage Page Cloning Using Kali Linux Social Engineering Toolkit

What is Burp Suite And its Features?