Tuesday, November 30, 2010

Pin It


Get Gadget

Useful Techniques in Assembly

How To Print Values Stored In Registers In Assembly

Most of the time when we use assembly, we load values to registers like AX,BX, Al,AH etc. Some time we need to print those values on the screen.

Before explain that we have to understand how the values are representing in the registers.

There are three type of representing values in a register,

1. Normal values(Pure decimal value convert in to binary and store in register)

For example, To store 68 we convert it to binary and its value is 1000100. So lets see the Value of each bit of AX register when we store that number in AX, AX is a 16bit register,

MSB 0000000001000100 LSB

2. As ASCII Values

For a Example Lets take we want to store "A8" in AX register; So the register saves the ASCII values in binary forum.

'A'=65=01000001
'8'=56=00111000

So the Values of AX Register is;

MSB 0011100001000001 LSB

3. Some Times We use Binary Coded Decimal To store VALUES.

0 = 0000
1 = 0001
2 = 0010
3 = 0011
4 = 0100
5 = 0101
6 = 0110
7 = 0111
8 = 1000
9 = 1001


So when we use to print a string stored in several registers as ASCII values we can use following technique to print values.

Varible_Name resd 16 ;Variable declaration in segment .bss section


mov [Varible_Name],eax ;move eax's four bytes to first 4 bytes of variable.
mov [Varible_Name],ebx ;move ebx's four bytes to first 4 bytes of variable.
mov [Varible_Name],ecx ;move ecx's four bytes to first 4 bytes of variable.
mov [Varible_Name],edx ;move edx's four bytes to first 4 bytes of variable.
mov si,Varible_Name
call _disp_str

_disp_str:
lodsb ; load next character
or al, al ; test for NUL character
jz .DONE
mov ah, 0x0E ; BIOS teletype
mov bh, 0x00 ; display page 0
mov bl, 0x07 ; text attribute
int 0x10 ; invoke BIOS
jmp _disp_str
.DONE:
ret

And when we want print a a 16bit value in binary in screen we have to move it to AX register and we can call this function. Then it will print the decimal value.

hex2dec:

push ax ; save that AX
push bx ; save that CX
push cx ; save that DX
push si ; save that SI
mov ax,dx ; copy number into AX
mov si,10 ; SI will be our divisor
xor cx,cx ; clean up the CX

non_zero:

xor dx,dx ; clean up the DX
div si ; divide by 10
push dx ; push number onto the stack
inc cx ; increment CX to do it more times
or ax,ax ; end of the number?
jne non_zero ; no? Keep chuggin' away

_write_digits:
pop dx ; get the digit off DX
add dl,48 ; add 48 to get ASCII
mov al, dl
mov ah, 0x0e
int 0x10
loop _write_digits

pop si ; restore that SI
pop cx ; restore that DX
pop bx ; restore that CX
pop ax ; restore that AX
ret ; End o' procedure!




No comments:

Post a Comment