/home -> /home/fatec/ -> /home/fatec/sistcomp -> /home/fatec/sistcomp/assembly --::-- |
IA-32 Assembly - Linux
Prof. Rossano Pablo Pinto, MSc.
Tools (check the documentation via man)
Lista de Programas em Assembly IA-32Tip: Verify the exit code specified at %ebx. After the execution of each program, execute the following command at the prompt: echo $? PROGRAM 1 ⇒ Prints the processor vendor: cpuid.s #cpuid.s Sample program to extract the processor Vendor ID #Author: Richard Blum .section .data output: .ascii "The processor Vendor ID is 'xxxxxxxxxxxx'\n" .section .text .globl _start _start: nop mov $0, %eax cpuid movl $output, %edi movl %ebx, 28(%edi) movl %edx, 32(%edi) movl %ecx, 36(%edi) movl $4, %eax # USES SYSCALL 4 (WRITE) TO PRINT TO SCREEN movl $1, %ebx # PRINTS TO STDOUT (FD 1) movl $output, %ecx # TEXT ADDRESS movl $42, %edx # TEXT LENGTH int $0x80 # INVOKES LINUX SYSCALL movl $1, %eax # USES SYSCALL 1 (EXIT) TO EXIT movl $0, %ebx # ERROR CODE = 0 int $0x80 # INVOKES LINUX SYSCALLGenerating the executable: as -gstabs -o cpuid.o cpuid.s ld -o cpuid cpuid.oRunning the program: ./cpuid The processor Vendor ID is 'GenuineIntel' PROGRAM 2 ⇒ Prints processor information: cpuinfo.s #cpuinfo.s Sample program to extract processor Info #Author: Rossano Pablo Pinto .section .data vendor: .ascii "The processor Vendor ID is 'xxxxxxxxxxxx'\n" # 42 bytes brand: .ascii " \n" # 49 bytes .section .text .globl _start _start: nop call vendorid call modelname call myexit vendorid: ########### VENDOR ID OPTION OF cpuid mov $0, %eax cpuid ########### PUT ADDRESS OF vendor variable INTO edi REGISTER movl $vendor, %edi movl %ebx, 28(%edi) movl %edx, 32(%edi) movl %ecx, 36(%edi) ########### CALL LINUX PRINT SERVICE - SYSTEM CALL movl $4, %eax movl $1, %ebx movl $vendor, %ecx ## POINTER TO START OF STRING movl $42, %edx ## STRING SIZE int $0x80 ret modelname: movl $brand, %edi movl $0x80000002, %eax cpuid movl %eax, 0(%edi) movl %ebx, 4(%edi) movl %ecx, 8(%edi) movl %edx, 12(%edi) movl $0x80000003, %eax cpuid movl %eax, 16(%edi) movl %ebx, 20(%edi) movl %ecx, 24(%edi) movl %edx, 28(%edi) movl $0x80000004, %eax cpuid movl %eax, 32(%edi) movl %ebx, 36(%edi) movl %ecx, 40(%edi) movl %edx, 44(%edi) ########### CALL LINUX PRINT SERVICE - SYSTEM CALL movl $4, %eax ##### WRITE SYSCALL movl $1, %ebx ##### STDOUT movl $brand, %ecx movl $49, %edx ## POINTER TO START OF STRING int $0x80 ## STRING SIZE ret myexit: ########### CALL LINUX EXIT SERVICE - SYSTEM CALL movl $1, %eax ##### EXIT SYSCALL movl $0, %ebx ##### EXIT CODE OF 0 int $0x80Generating the executable: as -gstabs -o cpuinfo.o cpuinfo.s ld -o cpuinfo cpuinfo.oRunning the program: ./cpuinfo The processor Vendor ID is 'GenuineIntel' Intel(R) Core(TM)2 CPU T5600 @ 1.83GHz PROGRAM 3 ⇒ Similar to 2: replace _start with main (uses gcc to generate code instead of "as" + "ld") . . . .globl main main: . . .Generating the executable: gcc -o cpuinfo cpuinfo.sRunning the program: ./cpuinfo The processor Vendor ID is 'GenuineIntel' Intel(R) Core(TM)2 CPU T5600 @ 1.83GHz PROGRAM 4 ⇒ Test the use of EFLAGS for conditional branch: cmptest.s # cmptest.s - An example with the instructions CMP and JGE # CMP sets EFLAGS accordingly # JGE verifies in EFLAGS if the value is greater than ... # # Theory: # Carry Flag (CF) - bit 0 (least significant bit) # Overflow Flag (OF) - bit 11 # Parity Flag (PF) - bit 2 # Sign Flag (SF) - bit 7 # Zero Flag (ZF) - bit 6 # # JGE - Jump if greater or equal (SF=OF) # .section .text .globl _start _start: nop movl $15, %eax movl $10, %ebx cmp %eax, %ebx jge MAIOR movl $1, %eax int $0x80 MAIOR: movl $20, %ebx movl $1, %eax int $0x80Generating the executable: as -gstabs -o cmptest.o cmptest.s ld -o cmptest cmptest.oRunning the program: ./cmptest Verify the exit code: echo $? 10 PROGRAM 5 ⇒ Test the use of EFLAGS for conditional branch: cmptest2.s # cmptest2.s - An example with the instructions CMP and JGE # CMP sets EFLAGS accordingly # JGE verifies in EFLAGS if the value is greater than ... .section .text .globl _start _start: nop movl $10, %eax movl $15, %ebx cmp %eax, %ebx jge MAIOR movl $1, %eax int $0x80 MAIOR: movl $20, %ebx movl $1, %eax int $0x80Generating the executable: as -gstabs -o cmptest2.o cmptest2.s ld -o cmptest2 cmptest2.oRunning the program: ./cmptest2 Verify the exit code: echo $? 20 Links- Art of Assembly Language Programming |