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:

test_func:
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

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

No comments:

Post a Comment