1 / 17

SOEN228, Winter 2003

SOEN228, Winter 2003. Revision 1.2 Date: October 25, 2003. Contents . Flags Mnemonics Basic I/O Exercises Overview of sample programs. Flag Register.

ziva
Télécharger la présentation

SOEN228, Winter 2003

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. SOEN228, Winter 2003 Revision 1.2 Date: October 25, 2003 Serguei A. Mokhov, mokhov@cs.concordia.ca

  2. Contents • Flags • Mnemonics • Basic I/O • Exercises • Overview of sample programs Serguei A. Mokhov, mokhov@cs.concordia.ca

  3. Flag Register • The flag register stores the condition flags that retain useful information of the past computation, especially as a consequence of the previous arithmetic operation. The following flags are noteworthy: • O Overflow • S Sign • Z Zero • P Parity (=1 if the number of 1’s in the result is odd) • C Carry (= carry-out from most significant bit) • I Interrupt (= 1 means interrupts are enabled; important in I/O programming, a future topic) • Flags are necessary to implement conditional jumps. • Recall the following from computer arithmetic: • 1000 + 1100 = 0100 O = 1; Z = 0; S = 0; P = 1; C = 1 Serguei A. Mokhov, mokhov@cs.concordia.ca

  4. 15 14 13 1211 10 9 8 7 6 5 4 3 2 1 0 Bit position 16-bit register Flags - - - - O D I T S Z - A - P - C Flags • A flag is a bit (0 or 1) in the flags register, a 16-bit register in the CPU. • There are nine flags: three control flags and six status flags. The remaining seven bits of the flags register are unused. • The three control flags are set (set means changes to 1) or reset (also called cleared, which means changes to 0) by instructions in the program. Serguei A. Mokhov, mokhov@cs.concordia.ca

  5. Serguei A. Mokhov, mokhov@cs.concordia.ca

  6. Compare Instruction Serguei A. Mokhov, mokhov@cs.concordia.ca

  7. Mnemonics • We will learn only a subset of the opcodes available. If you are interested, you could read about the rest. • Usually, in a current generation machine, the size of the instruction set can grow to several hundred instructions. • [What is the advantage of a large instruction set? Disadvantage?] Serguei A. Mokhov, mokhov@cs.concordia.ca

  8. Few Mnemonics Mnemonics Meaning add addition of two integers sub subtraction between two integers mul multiply two integers div divide one integer by another mov copy from source to destination inc increment operand by 1 dec decrement operand by 1 cmp compare two operands by sub andand of two operands oror of two operands xorxor of two operands ror or rol rotate shift right or left shr or shl logical shift right or left jmp jump to a different instruction loop repeat loop with cx as counter Mnemonics Meaning je jump if equal jz jump if zero jg jump if greater than jge jump if greater than or equal jl jump if less than jle jump if less than or equal jne jump if not equal jo jump if overflow push push operand onto stack pop pop operand from stack call jump to subroutine ret return from subroutine Serguei A. Mokhov, mokhov@cs.concordia.ca

  9. Basic I/O • We will make our lives easier by using some I/O functions supported by the OS. • These I/O functions are software traps (interrupts) whose invocation causes a transfer of control to the corresponding OS (software) interrupt handler. • These handlers can be considered as special subroutines that will be invoked whenever the int (software interrupt) instruction is executed. • In Linux, int 0x80 the specific interrupt that we will use. (In DOS, it is int 21h). • Parameters are passed from our program through registers from your program to the OS subroutine with a well-defined interface specification. Serguei A. Mokhov, mokhov@cs.concordia.ca

  10. Output • EAX = 4; 4 is a system call for Output • EBX = 1; the device, screen, or STDOUT • ECX = addr; starting address of the stuff to output; like a pointer to a buffer; char* • EDX = length of the buffer • Example: hello.asm Serguei A. Mokhov, mokhov@cs.concordia.ca

  11. Input • EAX = 3; • EBX = 0; Standard input or keyboard; STDIN • ECX = addr; address of the destination buffer • EDX = max # of bytes to enter • After int, EDX will have the length of the data actually read in bytes Serguei A. Mokhov, mokhov@cs.concordia.ca

  12. Program Termination • EAX = 1 • EBX = exit code; EBX = 0 means successful termination to the calling program. Serguei A. Mokhov, mokhov@cs.concordia.ca

  13. System Call Interface • A topic of the System Software and OS courses. • The goal is to abstract programmer from knowing hardware details when doing I/O. • Input is similar to read() system call in C • Output to write() • Termination to exit() • Whenever a system call is executed, it traps to kernel to carry out the requested operation on behalf of the user program using appropriate device driver. Serguei A. Mokhov, mokhov@cs.concordia.ca

  14. Exercises • Give an instruction that involves moving the content of some memory location to another memory location. • mov al, [mem_loc1] • mov [mem_loc2], al Serguei A. Mokhov, mokhov@cs.concordia.ca

  15. Exercises (2) • Write a program that moves 100 bytes from memory location input_buf to memory location output_buf. • See movbytes.asm Serguei A. Mokhov, mokhov@cs.concordia.ca

  16. Exercises (3) • Give a assembly language ‘instruction’ that does not correspond to a machine language instruction. • mov [output_buf], [input_buf] Serguei A. Mokhov, mokhov@cs.concordia.ca

  17. Exercises (4) • Write a program that accepts 25 characters from the keyboard and buffer them in memory location input_buf. • Afterwards, the characters are printed. • See printchars.asm. Serguei A. Mokhov, mokhov@cs.concordia.ca

More Related