[시스템프로그래밍]Assembler

윰지·2021년 3월 18일
0

시스템프로그래밍

목록 보기
2/2

프로그램 동작 원리


소스코드(hello.c) -> 전처리(preprocess) -> 전처리 후 소스(hello.i) -> Compiler -> Assembly소스(hello.s) -> Assembler -> 오브젝트파일(hello.o) -> Linker -> 실행파일(hello.out) -> Loader -> Memory

  1. Preprocessor
    전처리기 구문(#으로 시작하는 구문)을 처리하는 것이다. 예를 들어 #include가 있다.
  2. Compiler
    High-level language를 low-level language로 나타낸다. Low-level language을 기계어와 가장 가까운 언어이다.
  3. Assembler
    어셈블리 언어로 작성된 소스 코드를 기계어로 바꾸어 주는 역할을 한다.
  4. Linker
    어셈블러에 의해 생성된 파일들을 하나의 실행파일로 합친다.

Asembler란?

어셈블리 language로 작성된 코드를 machine 코드로 변환하는 일을 한다.(.o파일 생성) 이때 object 파일은 instruction과 data, 그리고 memory에 어디에 있는지에 대한 정보를 담고 있다. 어셈블러의 기본적인 역할은 operation code를 거기에 상응하는 machine language equivalent로 변환한다. 또한 assigned machine addresses to symbolic labels used by the programmer
opcode 뿐만 아니라 operand도 변환
그 과정에서 label에 대한 address를 찾아야 하고 symbol table이라고 하는 data structure를 사용한다.

SIC Assembler Language Program


Assembler Directive
Directive가 사용된 것은 실제로 머신코드로 변환되는 부분이 아니다.

  • START : 프로그램의 이름과 시작 주소를 명시한다.
  • END : 원시 프로그램(source program)의 끝을 나타내며 (선택적으로) 최초로 실행할 명령어를 지시한다.
  • BYTE : 문자나 16진수 상수를 생성하며 상수를 표현하는데 필요한 만큼의 바이트를 사용할 수 있다.
  • WORD: 1워드의 integer 상수를 생성한다.
  • RESB : 데이터 영역의 크기를 지시된 바이트 수 만큼 예약한다.
  • RESW : 데이터 영역의 크기를 지시된 워드 수 만큼 예약한다.

Example 1

  • Main routine : F1이라는 Input 장치에서 record를 메모리로 불려들어오고 그것을 05라고 하는 output 장치에 쓰는 것이다.
  • Two subroutines
    RDREC : buffer에 써서 record를 read한다.
    WRREC : buffer에 있는 것을 output에 쓴다.
  • 각각의 record는 마지막에 00이라는 null charcter 값으로 마지막임을 표현한다.
  • RSUB로 프로그램이 종료된다.

이 어셈블리 코드가 COPY라는 소스코드 이름을 가지고 있고 시작주소는 1000으로 가정하고 있다. 실질적인 instruction은 line10부터 시작한다.

Program with Object Code

  • 'Loc'은 각 부분의 machine address(16진수)를 의미한다.
  • Translate source program into object codes
    1) Convert mnemonic operation codes to their machine language equivalents -> STL을 14로(line10)
    2) Convert symbolic operands to their equivalent machine address -> RETADDR을 1033으로(line10)
    3) Build the machine instructions
    4) Covert data constants specified in the source program into their internal machine representations -> EOP를 454F46으로(line80)
    5) Write the object program and the assembly listing
  • 하지만 2)의 과정이 쉽지 않다. Forward reference 때문이다. 따라서 대부분의 어셈블러는 2번의 scanning 과정을 거치게 된다.
    • The 1st pass : scan
    • The 2nd pass : 실질적인 translation

Object Program Corresponding


각 line별로 object가 어떻게 변환되는지를 봤다면 결국 이게 하나의 object 파일로 만들어져서 output device에 쓰이고 loader에 의해 메모리에 적재된다.

object program format

  • Header record
    프로그램 이름, 시작 주소, 길이 표현한다.
Col의미
Col. 1H
Col. 2-7Program name
Col. 8-13Starting address of object program(hex)
Col. 14-19Length of object program in bytes(hex)
  • Text record
    변환된 instruction과 data를 포함한다. 실제 로딩될 machine 주소 값을 포함한다.
Col의미
Col. 1T
Col. 2-7Starting address of object code in this record(hex)
Col. 8-9Length of object code in this record in bytes(hex)
Col. 10-69Object code, represented in hex(2 columns per byte of object code)
  • End record
    object program의 마지막을 의미한다. 실제 실행이 되야하는 첫번째 instruction을 표현하는 정보를 포함한다.
Col의미
Col. 1E
Col. 2-7Address of first executable instruction in object program(hex)

Object program corresponding to Example 1
107A : 1000부터 2079에서 05라는 1byte를 더하면 된다.

2-pass Assembler

Pass 1 : Define symbols
symbol 혹은 label을 define하는 작업을 한다. Pass1은 intermediate file에 결과물을 쓴다.

  1. 어셈블리 코드를 한줄씩 읽는다.
  2. Address를 byte 길이만큼 혹은 word 길이만큼 증가시키면서 할당한다.(Loc)
  3. Symbol table에 label에 대한 address값을 저장한다.(실제 사용은 2-pass에서)
  4. Constant declaration(BYTE, WORD)이나 space reservation(RESB, RESW)과 같은 assembler directive를 처리한다.

Pass 2 : Assemble instructions & generate object program
실제 instruction에 대한 assemble과정을 진행하고 object file을 만든다.

  1. Input file은 pass1에서 생성된 intermediate file이고 각 라인에 있는 코드를 읽는다.
  2. 실질적인 operation code(mnemonic)의 translation이 이루어진다.(OP Code Table 사용)
  3. Operand의 label을 address로 바꿔준다.(Symbol Table 사용)
  4. pass 1에서 수행되지 않은 assembler directives가 있다면 처리한다.
  5. Object program을 생성한다.

Two Data Structure of SIC Assembler

Operational Code Table(OPTAB)

  • pass 1에서 mnemonic code를 찾기만 하고 pass 2에서 machine language로 번역한다.
  • hash table로 이루어진다.
    • key : mnemonic code
    • fast retrieval
    • 한번 적히면 바뀌지 않는다.

Symbol Table(SYMTAB)

  • 크게 symbol의 name과 value(address)가 적힌 필드가 있다.
  • pass 1에서 symbol table을 채우는 역할을 하고 pass 2에서 이를 사용하여 실질적인 address를 바꾼다.

Example 1(ver. SIC/XE Assembly Program)

Register-to-register instruction을 사용한다. Immediate&Indirect addressing을 될 수 있는 한 많이 사용한다. 이를 통해 프로그램 실행 속도를 빠르게 할 수 있다. 성능을 높일 수 있다. Immediate를 할 경우 memory reference하는 시간을 줄이기 때문이다.

  • Indirect addressing
    @ ex) J @RETADR
  • Immediate operands
    # ex) COMP #0
  • Format 4 instruction
    + ex) +JSUB RDREC
  • Base relative addressing
    BASE ex) BASE LENGTH


Program Relocation


Multiprogramming을 할 때 즉, time sharing 기법을 통해 하나 이상의 프로그램을 사용하고 싶을 때 유용한 방식이 될 수 있다. 메모리에 공간이 있을 때 이를 적절히 활용한다. Assembler가 직접 메모리에 로딩하는 역할을 수행(이는 로더의 역할)하지는 않으므로 어려움이 있다.
시작 주소에 따라 실제 symbol의 address값이 달라질 수 있다. 따라서 시작 주소가 달라지면 이를 반영할 수 있어야 하는데 assembler는 어디부터 시작하여 값을 바꿔야 하는지 알지 못한다. 이때 assembler가 할 수 있는 것은 '실제 주소가 들어오면 바뀔 수 있다.'라는 정보를 담으면 된다. 그러면 로더가 수정을 해서 실제 시작주소로부터 떨어진 값을 넣는다.

Col의미
Col. 1M
Col. 2-7Starting location of the address field to be modified, relative to the beginning of the program(hex)
Col. 8-9Length of the address field to be modified, in half-bytes(hex)

Machine-Independent Assembler Feature


특정 컴퓨터 구조와 관계없이 공통적으로 적용 가능한 어셈블러의 특성들이다.

  1. Literals
    Constant value를 직접적으로 표현할 수 있는 방법이다. 실제 machine instruction에 직접 표현되는 것은 아니고(immediate addressing) 메모리 location에 constant를 잡고 TA를 disp에 활용하는 부분은 기존과 동일하다.
    프로그램의 다른 곳에서 상수를 정의하고 레이블을 붙이는 불편을 덜어준다. 이를 통해 가독성을 높이기 위해 사용되고 더 높이기 위해서는 literal pool(LTORG)을 사용한다.

    • "="
    • LITTAB(Literal Table) : literal name, operand value and length, address assigned to the operand when it is placed in a literal pool라는 3개의 field가 포함된다. Pass1, pass2로 알고리즘이 만들어진다.
  2. Symbol Definitions
    어셈블러들은 프로그램 directive를 제공하는데 프로그래머가 symbol을 define하고 그 symbol의 value를 지정할 수 있는 directive(EQU(equate))를 제공한다. EQU는 absolute and relative 다 가능

    • Form : symbol EQU value
    • Value를 선언하는 부분이 먼저 나와야 한다.
      ex) ALPHA RESW 1 -> BETA EQU ALPHA 순으로 해야한다.
  3. Expression
    Operand를 선언하는 곳에 수식을 표현할 수 있다. Assembler가 수식을 결국 계산할 수 있다.
    ex) BUFEND EQU * : buffer area 다음에 나오는 next byte address

    • Absolute expressions : independent of program location
    • Relative expressions : relative to the beginning of program
  4. Program Block
    within a single object program unit

  • USE : program block을 사용하기 위한 directive
  • Pass1 : 분리한 LOCCTA를 유지한다. SYMTBL에 block number를 저장한다.
  • Pass2 :
Block NameBlock NumberAddressLength
default000066
CDATA166000B
CBLKS2711000
  1. Control Section
    independent object program units

0개의 댓글