Tuesday, April 5, 2011

How to print a word (two bytes) in assembly

assume the data that need to be printed are in DX register. CX(CL) is the counter to be used to shift the bit of DX. At the end of the code, ascii 13 + ascii 10 are equivalent to "\r\n"

movb $16, %cl
subb $4, %cl
movw %dx, %ax
shr %cl, %ax
andw $0x000f, %ax
cmpb $10, %al
jl test_word_less
addb $7, %al #prepare for A,B,C...because 17+48=65(A)

addb $48, %al
movb $0xe, %ah
movw $7, %bx
int $0x10
cmpb $0, %cl
jne test_word_loop
movb $13, %al
int $0x10
movb $10, %al
int $0x10

Monday, April 4, 2011

How to print register value in assembly language

Here is the AT&T assembler function I came up with in order to print something during the Linux kernel debugging. Assuming the lower FOUR bits of CX (CL) are use as a value we want to print a HEX of, according to ascii table:

andb $0x0f, %cl #mask the higher four bits
cmpb $10, %cl #see if we need 1,2,3,4...or A,B,C,D...
jl test_less #if the value is less than 10, jump
subb $10, %cl #if the value if larger or equal to 10, offset it
addb $17, %cl #prepare for A,B,C...17+48=65, can use addb %3, %cl as well

addb $48, %cl #Then the result is stored in CL, call int 0x10 to print it