Author Topic: My PrintInt.asm and PrintString.asm  (Read 19085 times)

Offline Shikatsu

  • Jr. Member
  • *
  • Posts: 10
My PrintInt.asm and PrintString.asm
« on: March 03, 2012, 02:58:53 AM »
Greetings EveryOne ;D

I would like to share with you my 16-bit assembly code as an example to print an integer and a string to the standard output:

PrintInt.asm:
Code: [Select]
;nasm -fbin PrintInt.asm -o PrintInt.com

org 100h

segment .text
pusha
mov     EAX, 5473   ;save our interger to EAX
call    print_int   ;call print_int routine
popa

mov     EAX, 4C00h  ;instruct to exit program
int     21h

segment .text
print_int:
pusha           ;push all registers to the stack
mov     ECX, 0

countDigits:
cdq             ;This instruction is available only
                ;on the 80386 and later
mov     EBX, 10
idiv    EBX     ;divide EAX by EBX,
                ;the result is saved as: EAX:EDX

push    EDX     ;save the fractional part of
                ;the fraction above in the stack.
                ;the digits of our integer are saved
                ;like this: 3, 7, 4, 5

inc     ECX     ;increament ECX, it tracks the number
                ;of digits in our integer

cmp     EAX, 0
jnz     countDigits ;when EAX is 0 means we had pushed
                    ;every digit of our integer to
                    ;the stack

show_int:
pop     EDX         ;retrive the saved digits on the
                    ;stack and load them to EDX.
                    ;our digits are retrived from the
                    ;stack in this order: 5, 7, 4, 3
                    ;remember Last In First Out.

add     EDX, 30h    ;convert our retrived digt to ascii
mov      AH, 2h     ;2h is an interrupt when passed to AH
                    ;that instruct the processor to print
                    ;to standart output the ascii value
                    ;saved in EDX.
int     21h         ;instruct the processor to execute
                    ;the interrupt above.

dec     ECX         ;at this point we have one less digit
                    ;to display.
cmp     ECX, 0
jnz     show_int

popa                ;retrive the register values that been
                    ;pushed to the stack with "pusha"
ret                 ;return

Note: It's safer to zero EAX before moving a value to one of EAX chunks to pass the integer we want to print_int, something like:

Code: [Select]
mov     byte [Var], 5
xor     EAX, EAX
mov     AL, byte [Var]
call    print_int

PrintString.asm:
Code: [Select]
;nasm -fbin PrintString.asm -o PrintString.com

org 100h

segment .text
mov     EAX, msg
call    print_string

mov     AX, 4C00h
int     21h


segment .text
print_string:
pusha
mov     EDX, EAX
mov     AH, 9h      ;if you move 9h to AL
                    ;the prog will hang for
                    ;user input
int     21h
popa
ret

segment .data
msg     DB      'Hello, I am an Assembly Programmer:D', 24h

If there is any feedback I would love to hear it, Thank You.

Enjoy, Shikatsu ;D
« Last Edit: March 28, 2012, 08:28:41 PM by Shikatsu »

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
Re: My PrintInt.asm and PrintString.asm
« Reply #1 on: March 03, 2012, 03:52:15 AM »
I question the comment in this section:

Code: [Select]
mov     EDX, EAX
mov     AH, 9h      ;if you move 9h to AL
                    ;the prog will hang for
                    ;user input
int     21h

Does this actually work for you?

Best,
Frank


Offline Shikatsu

  • Jr. Member
  • *
  • Posts: 10
Re: My PrintInt.asm and PrintString.asm
« Reply #2 on: March 03, 2012, 04:16:10 AM »
I question the comment in this section:

Code: [Select]
mov     EDX, EAX
mov     AH, 9h      ;if you move 9h to AL
                    ;the prog will hang for
                    ;user input
int     21h

Does this actually work for you?

Best,
Frank

I double checked, for me yes the program hang for user input (single char input) and echo the pressed key to the standard output.