1.2.3 - Introduction to Programming

Cards (29)

  • Procedural Programming
    • widely-used paradigm
    • can be used for a wide range of problems
    • fairly easy to write/interpret
    • uses sequence of instructions usually contained in procdeures
    • these are carried out step by step
  • Program Flow - Structures
    • sequence
    • selection
    • iteration
  • Sequence
    • code executed line-by-line, top to bottom
  • Selection
    • block of code is run if a certain condition is met
    • IF, ELSE IF and ELSE statements
  • Iteration
    • block of code is executed a select number of times or while a condition is met
    • FOR, WHILE or REPEAT UNTIL loops
  • Variables
    • named locations in memory where data is stored
    • contents of location can be changed while program is being executed
    • must be declared before use
    • sometimes need to be assigned a data type
  • Constants
    • named locations in memory
    • value cannot be changed by program during execution
    • used for values that don't need to be changed (Pi, VAT)
    • prevents value from being changed accidentally
    • often capitalised (PI, VAT)
  • Procedures
    • named blocks of code that perform a specific task
    • do not have to return a value
    • can return multiple values
    • usually given data as parameters
  • Functions
    • named blocks of code that perform a specific task
    • must always return a value
    • can only return a single value
    • often use local variables
  • Arithmetic Operators
    • used to carry out math functions in programs
  • Arithmetic Operators - Examples
    • +, -, *, /
    • **
    • DIV or //
    • MOD or %
  • **
    • exponentiation - raising a number to a power
    • 2**4 = 16
    • 2^4 = 16
  • DIV or //
    • calculates whole number of times a number goes into another
    • integer division
    • 50 DIV 7 = 7
  • MOD or %
    • used to find the remainder when a number is divided by another
    • 50 MOD 7 = 1
  • Relational Operators
    • make comparisons between two values
    • produce result of True or False
  • Relational Operators
    • > greater than
    • < less than
    • >= greater than or equal to
    • <= less than or equal to
    • != not equal to
    • == equal to
  • Assembly Language
    • uses mnemonics instead of binary
    • each mnemonic is represented by a numeric code
    • commands used are processor-specific as they interact directly with CPU's special purpose registers
    • allows for direct interaction with hardware - useful in embedded systems
  • Assembly Language - Mnemonics
    • ADD
    • SUB
    • STA
    • LDA
    • INP
    • OUT
    • HLT
    • DAT
    • BRZ
    • BRP
    • BRA
  • ADD
    • add
    • adds value at given memory address to value in accumulator
  • SUB
    • subtract
    • subtracts value at given memory address from value in accumulator
  • STA
    • store
    • stores value in accumulator at given memory address
  • LDA
    • load
    • load value at given memory address into accumulator
  • INP
    • input
    • allows user to input a value that gets held in the accumulator
  • OUT
    • output
    • prints value currently held in the accumulator
  • HLT
    • halt
    • stops program at that line, stopping the rest of the code from executing
  • DAT
    • data
    • creates a flag with a label at which data is stored
  • BRZ
    • branch if zero
    • branches to given address if value in accumulator is zero
    • conditional branch
  • BRP
    • branch if positive
    • branches to given address if value in accumulator is positive
    • conditional branch
  • BRA
    • branch always
    • branches to given address no matter the value in the accumulator
    • unconditional branch