Thursday, November 4, 2010

AT&T assembly examples

In this blog, I am going to give some example of assembly language.

movb  $0x11, 0xa0000 // this is going to write 1 byte 0x11 to memory 0xa0000
movw $0xa000, %dx // this is going move 2 bytes 0xa000 to register dx
movl  $0x0000ffcc, %eax // this is going to move 4 bytes to register eax

/*write some ports*/
movb $0x11, %al
movw $0x80, %dx
outb %al, %dx
// this is going to write a single byte at the port 0x80
// us dx to store the port number seems like the convention
// if it is in: inb %dx, %al // read from port %dx, and store it in %al


/*write to serial port 0x3f8*/
movb $0x11, %al
movw $0x3f8, %dx
outb %al, %dx
// this code if you don't move serial port number to dx, just using outb %al, $3f8
// this will not work. I don't know why, but I think I have tested.

/*use a loop to do copy*/

movl $0xa0000, %eax
movl $smm_handler_start, %ebx
movl $smm_handler_end, %ecx
subl $smm_handler_start, %ecx
mylabel:
             movb (%ebx), %dl
             movb %dl, (%eax)
             inc %eax
             inc %ebx
             loop mylabel

// this code is going to copy content from [smm_hanlder_start, smm_handler_end) to memory address 0xa0000. smm_handler_start and smm_handler_end are label in another .S file. and they need to declare it as .global, and current file need to declare it as .extern. the number of times the loop will execute is the value at ecx. but in C is different. For example:
foo.S
.global smm_handler_start

smm_handler_start:
...
...


bar.c
extern uint_8 smm_handler_start
// if you want to get the address of smm_hanlder_start
// you need to use it like: &smm_hanlder_start (= $smm_hanlder_start in asm)

No comments: