Your SlideShare is downloading. ×
0
lesson02.ppt
lesson02.ppt
lesson02.ppt
lesson02.ppt
lesson02.ppt
lesson02.ppt
lesson02.ppt
lesson02.ppt
lesson02.ppt
lesson02.ppt
lesson02.ppt
lesson02.ppt
lesson02.ppt
lesson02.ppt
lesson02.ppt
lesson02.ppt
lesson02.ppt
lesson02.ppt
lesson02.ppt
lesson02.ppt
lesson02.ppt
lesson02.ppt
lesson02.ppt
lesson02.ppt
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

lesson02.ppt

507

Published on

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
507
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
4
Comments
0
Likes
0
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. GNU/Linux assembly language Reviewing the basics of assembly language programming for Intel x86-based Linux systems
  • 2. Our motivations <ul><li>We want to study the new Intel processor technologies for Core-2 Duo & Pentium-D: </li></ul><ul><ul><li>EM64T (Extended Memory 64-bit technology) </li></ul></ul><ul><ul><li>VT (Virtualization Technology) </li></ul></ul><ul><li>These capabilities are optionally ‘enabled’ during the system’s ‘startup’ phase, so we will want to execute our own ‘boot loader’ code to have control at the earliest stage </li></ul>
  • 3. ‘boot’ code <ul><li>On PC systems the mechanism for doing the IPL (‘Initial Program Load’) requires a ‘boot-loader’ program that can fit within a single 512-byte disk-sector </li></ul><ul><li>High-level programming languages (such C/C++) typically employ compilers which generate object-code files whose size is too large to be stored in a disk-sector </li></ul>
  • 4. Portability implications <ul><li>Also ‘High-Level’ languages typically have platform ‘portability’ as a key design-goal, but they can’t achieve that requirement if architecture-specific language-constructs are employed; thus they usually offer to a systems programmer only those features in the ‘lowest common denominator’ of the various different computer-systems </li></ul>
  • 5. Intel’s VMX instructions <ul><li>So when we want to explore Intel’s VM-x technology (Virtualization for x86 CPUs), we will need to use ‘low-level’ computer language (i.e., assembly language) </li></ul><ul><li>The advantage, of course, is that we will thereby acquire total control over the CPU in any Intel-based computer that supports the new so-called VMX instruction-set. </li></ul>
  • 6. Simplified Block Diagram Central Processing Unit Main Memory I/O device I/O device I/O device I/O device system bus
  • 7. The sixteen x86 registers EAX ESP EBX EBP ECX ESI EDX EDI EIP EFLAGS CS DS ES FS GS SS Intel Pentium processor
  • 8. The x86 EFLAGS register Legend: ZF = Zero Flag SF = Sign Flag IOPL = I/O Privilege-Level (0,1,2,3) CF = Carry Flag AC = Alignment-Check (1=yes, 0=no) PF = Parity Flag NT = Nested-Task (1=yes, 0=no) OF = Overflow Flag RF = Resume Flag (1=yes, 0=no) AF = Auxiliary Flag VM = Virtual-8086 Mode (1=yes, 0=no) VIF = ‘Virtual’ Interrupt-Flag VIP = ‘Virtual’ Interrupt is Pending ID = the CPUID-instruction is implemented if this bit can be ‘toggled’ 0000000000 I D V I P V I F A C V M R F 0 N T IOPL O F D F I F T F S F Z F 0 A F 0 P F 1 C F 31 22 21 20 19 18 17 16 15 14 13-12 11 10 9 8 7 6 5 4 3 2 1 0 = ‘reserved’ bit = ‘status’ flag (for application programming)
  • 9. Our ‘eflags.s’ demo <ul><li>This program shows the EFLAGS bits at the moment the program began executing </li></ul><ul><li>It’s instructive to run this program before you execute our ‘iopl3’ command, and do so again after you execute that command </li></ul>
  • 10. program translation steps program source module eflags.s program object module assembly eflags.o the executable program object module library object module library other object modules linking eflags Not every program requires additional object-modules
  • 11. program translation commands <ul><li>To ‘assemble’ your source-file: </li></ul><ul><li>To ‘link’ the resulting object-file: </li></ul><ul><li>To ‘run’ your executable program: </li></ul>$ as eflags.s –o eflags.o $ ld eflags.o –o eflags $ ./eflags
  • 12. Last night (Wed, 24 Jan 2007) Alex Fedosov wrote: > > Subject: Re: hardware > > I checked #11-#12 serial cable and it seems that it was > loose. Let me know if it continues to be a problem. > > Good news! Your machines are here! We will be building > them tomorrow. > > -a
  • 13. Our ‘feedback.s’ demo <ul><li>We wrote an example-program that shows how you could program the serial UART in the GNU/Linux assembly language </li></ul><ul><li>It uses the x86 ‘in’ and ‘out’ instructions to access the UART’s i/o-ports: </li></ul><ul><ul><li>0x03F8: port-address for Divisor-Latch </li></ul></ul><ul><ul><li>0x03FB: port-address for Line-Control </li></ul></ul><ul><ul><li>0x03FD: port-address for Line-Status </li></ul></ul><ul><ul><li>0x03F8: port-address for RxD and TxD </li></ul></ul>
  • 14. Statement layout <ul><li>Parsing an assembly language statement </li></ul><ul><ul><li>A colon character (‘:’) terminates the label </li></ul></ul><ul><ul><li>A white-space character (e.g., blank or tab) separates the opcode from its operand(s) </li></ul></ul><ul><ul><li>A hash character (‘#’) begins the comment </li></ul></ul>label: opcode operand(s) # comment
  • 15. How ‘outb’ works <ul><li>Three-steps to perform ‘outb( data, port );’ </li></ul><ul><ul><li>Step 1: put port-address into DX register </li></ul></ul><ul><ul><li>Step 2: put data-value into AL register </li></ul></ul><ul><ul><li>Step 3: output data-value to port-address </li></ul></ul>mov $0x03FB, %dx # UART’s Line_Control register mov $0x80, %al # value for setting the DLAB bit out %al, %dx # output data-byte to the i/o-port
  • 16. How ‘in’ works <ul><li>Three steps to perform ‘data = in( port );’ </li></ul><ul><ul><li>Step 1: put port-address into DX register </li></ul></ul><ul><ul><li>Step 2: input from port-address to AL register </li></ul></ul><ul><ul><li>Step 3: assign value in AL to location ‘data’ </li></ul></ul>mov $0x03FD, %dx # UART’s Line_Status register in %dx, %al # input from i/o-port to accumulator mov %al, data # copy accumulator-value to variable
  • 17. How to receive a byte Read the Line Status Register Read byte from the Receiver Data Register Received Data is Ready? NO YES DONE
  • 18. Rx implementation # This assembly language code-fragment inputs a byte of data from # the remote PC, then assigns it to a memory-location labeled ‘inch’ mov $0x03FD, %dx # UART’s Line_Status register spin1: in %dx, %al # input the current line-status test $0x01, %al # is the RDR-bit zero? jz spin1 # yes, no new data received mov $0x03F8, %dx # UART’s RxData register in %dx, %al # input the new data-byte mov %al, inch # store the data into ‘inch’
  • 19. How to transmit a byte Read the Line Status Register Write byte to the Transmitter Data Register Transmit Holding Register is Empty? NO YES DONE
  • 20. Tx implementation # This assembly language code-fragment fetches a byte of data from # a memory-location labeled ‘outch’, then outputs it to the remote PC mov $0x03FD, %dx # UART’s Line_Status register spin2: in %dx, %al # input the current line-status test $0x20, %al # is the THRE-bit zero? jz spin2 # yes, transmitter is still busy mov $0x03F8, %dx # UART’s TxData register mov outch, %al # get the value to transmit out %al, %dx # output byte to TxD register
  • 21. Initializing the UART Set the Divisor Latch Access Bit in the Line Control Register Write a nonzero value to the Divisor Latch Register Clear the Divisor Latch Access Bit and specify the desired data-format in the Line Control Register DONE BEGIN
  • 22. Init-UART implementation # This assembly language code-fragment initializes the UART for # ‘polled-mode’ operation at 115200 baud and 8-N-1 data-format mov $0x03FB, %dx # UART Line_Control register mov $0x80, %al # set the DLAB-bit (bit 7) to 1 out %al, %dx # for access to Divisor_Latch mov $0x03F8, %dx # UART Divisor_Latch register mov $0x0001, %ax # use 1 as the divisor-value out %ax, %dx # output the 16-bit latch-value mov $0x03FB, %dx # UART Line_Control register mov $0x03, %al # set data-format to 8-N-1 out %al, %dx # establish UART data-format
  • 23. In-class exercises <ul><li>You can try assembling, linking, and then running our ‘feedback.s’ demo-program (use the ‘trycable’ program to send some characters via the ‘null-modem’ cable) </li></ul><ul><li>Can you modify this ‘feedback.s’ program so that it will continuously display and transmit every character it receives? </li></ul>
  • 24. Exercise illustration null-modem cable Start our ‘feedback’ program running on this workstation Then run our ‘trycable’ program on this other workstation Now watch what happened on these two screens

×