DOC PREVIEW
Princeton COS 318 - Real mode

This preview shows page 1-2-3-23-24-25-26-47-48-49 out of 49 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 49 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 49 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 49 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 49 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 49 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 49 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 49 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 49 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 49 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 49 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 49 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

x86_16 real mode (or at least enough for cos318 project 1)OverviewPreliminary information - How to find helpThe toolchainThe machineIf you only remember one thing: gcc -Sthe -S (capital S) flag causes gcc to ouput assembly.Preliminary InformationAssembly can be hardDevelopment strategies conquer risk:Write small test cases.Write functions, test each separately.Print diagnostics frequently.Think defensively!and the interweb is helpful too.The Interwebs as a resource.The internet offers much information that seems confusing or contradictory.How do you sort out information "in the wild?"SyntaxThere are (at least) two different syntaxes for x86 assembly language: AT&T and Intel. AT&T: opcodes have a suffix to denote data type, use sigils, and place the destination operand on the right. Intel: operands use a keyword to denote data type, no sigils, destination operand is leftmost.Example: AT&T vs Intelpush %bpmov %sp,%bpsub $0x10,%spmovw 0x200b(%bx),%simov $0x4006,%dimov $0x0,%axcall printfleaveqretqIn this class, use AT&T!push bpmov bp,spsub sp,0x10mov si,WORD PTR [bx+0x200b]mov di,0x4006mov ax,0x0call printfleaveretVersions of the architecturex86 won't die. All backwards compatible.8086 -> 16bit, Real80386 / ia32 -> 32bit, Protectedx86_64 -> 64bit, ProtectedIf you find an example: For which architecture was it written?The Register TestIf you see "%rax", then 64-bit code; elseIf you see "%eax", then 32-bit code; elseYou are looking at 16-bit code.OverviewPreliminary information - How to find helpThe toolchainThe machineThe toolchainThe lab has all the software you need. You can connect remotely via ssh -X labpc-yy All software is available for free on *nix, Mac OS X, and probably windows. If you use a 64-bit machine, you may have problems.Ask me offline.Text editorsYou should know how to use an editor vi and emacs are popular choices......and you should learn them, if for no other reason than to understand geek jokes.s/bug/feature/M-x psychoanalyze-pinheadThe Assembler: as or gasThe cycle:You write an assembly language text file (.s)run: as --32 -g source.s -o obj.oA disassembler is also useful:objdump -D -M i8086 obj.o > obj.s We have provided a makefile to make this painlessbochs bochs ("box") is a free, open-source emulator of a complete PC How do we use it?Bochs treats a file as a disk in the emulated computer.The computer will boot off of it.bochs will be discussed more in later precepts.OverviewPreliminary information - How to find helpThe toolchainThe machineScopeThis is not an exhaustive list of x86 features.It's just enough to get you rolling. In fact, I want to discourage some of the more advanced uses. If you keep it simple, it will be easier to develop, debug, and grade.Again: gcc -Sgcc -S -m32 - fomit-frame-pointer test.c -o test.sAbout optimizing your code.DON"T OPTIMIZE YOUR CODE!!!111!!!!I will have to read your code. Please keep-it-simple. Memory access in separate instructions.Use .EQU to give names to constants. Comments that say what you're trying to do.Caution: x86 is wonky.a lot of instructions, many redundant. very few registers, and funny rules about what each may do. Real vs Protected modes; Segmented Memory! Here, we focus on a sane subset of x86.The syntax of a .s file# commentRegister names have the %-sigil, eg %axLiterals have the $-sigil, eg $0x1234Literals without the $-sigil mean memory! label:Instructions may have suffixes -b (byte, 8-bit) or -w (word, 16-bit).x86_16 RegistersGeneral purpose registers:%ax, %bx, %cx, %dx%a h is the most-significant byte%a l is the least. Pointer registers:%si, %di, %sp, %bp, %ipSegment registers:%ds, %es, %cs, %ssControl register:%flagsSegmented Memory on x86Good news: you can mostly ignore it at the local instruction level. Bad news: you need to understand it to complete this project. Why is it here? In the good-ole' days...pointers were small, andwe didn't have memory management units.Segmented Memory: Why?Some machine instructions must contain memory locations.But, your compiler cannot know what other programs are running......or what addresses they use.A layer of abstraction between instructions and physical memory solves this problem.Put the code anywhere in physical memory, but give it the logical address it desires.Segmented Memory on x86Segmented memory is a hack.Makes pointers slightly larger.Provides rudimentary support for relocation. Intel's solution:Memory is many overlapping segments.A pointer is an address within a segment.A segment register adds 4-bits to the address space.Segmented Memory on x86Suppose segment register %ds holds a segment numberSuppose register %bx holds an address.Then %ds:%bx is a logical memory address. The physical address in memory is:%ds:%bx == 16 * %ds + %bx The pointer is 4 bits wider.Segments as RelocationObserve that:x:y == (x+1):(y-16)x:y == (x-1):(y+16) Say you have code that assumes it is at memory address zero......but, we're using address zero for something else...Adjust segment registers, and give the illusion that the code is at the desired address.How segments help us in P1The bootloader must move itself to another physical memory location, as to make room for the kernel.Segmentation allows us to move, but keep logical memory addresses the same.How segments hurt us in P1If the kernel is bigger than a segment (64KiB), then you will need to perform several disk reads to different segments :(This is why support for >128 sectors is extra credit.Practical Ex. of SegmentsFor project 1, we write bootblock.sThe assembler assumes logical address 0, but on x86 that address is reserved.Instead, BIOS loads the bootloader to 0x0:0x7c00Although the physical memory address has changed, 0x0:0x7c00==0x07c0:0x0. If you read/write memory through segment 0x07c0, everything works as usual...Practical Ex. of SegmentsWe want to the kernel at physical address 0x0:0x1000.If the kernel is >27KiB,then boot loader and kernel overlap!Need to relocate the boot loader.x86 InstructionsNext, I'm going to show a bunch of instructions and their semantics.I'll write a general form, then the RTL semantics.MemoryStacksArithmeticControlx86: Memorymovw ptr, rr ← Mem[ptr] (16-bit)movw r,ptrMem[ptr] ← r (16-bit)where, ptr is an address expression:0x1234 - absolute address (no $-sigil)(r) - address specified in register.0x1234(r) - r+0x1234etcIn segment %ds by default!x86: More Memorylodsw%ax ← Mem[ %ds:%si ]%si++movsw%Mem[ %es:%di ] ← %Mem[


View Full Document

Princeton COS 318 - Real mode

Documents in this Course
Overview

Overview

25 pages

Deadlocks

Deadlocks

25 pages

lectute 2

lectute 2

28 pages

Lecturel

Lecturel

24 pages

Lecture 2

Lecture 2

54 pages

lecture 5

lecture 5

27 pages

Load more
Download Real mode
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Real mode and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Real mode 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?