Chapter 3.1: Programming Languages

Chapter 3.1: Programming Languages · Study Questions
Language levelsMachine languageInstruction formatAssembler and loaderListing outputSyntaxesSystem callsStack and stdcallCompiled and interpretedfor/if → 8086
Sercan KÜLCÜ · Giresun University · Computer Engineering

Multiple Choice · Software

Question 1: Language Levels

As we move up from machine language to assembly and then to high-level languages, what happens?

B: In a high-level language the compiler decides which register or address a variable lives in; the same source code can be recompiled for different processors. The price is less direct control of the hardware. The source still has to be translated to machine language by a compiler or interpreter; choosing addresses and registers by hand is a feature of assembly.

Coding Question · Software

Question 2: The Same Operation at Three Levels

The C code y = a - b + c;: write it in 8086 assembly (16-bit variables at addresses a = 0200h, b = 0202h, c = 0204h, y = 0206h). Then derive the machine code using the codes below. If a = 50, b = 8, c = 3, what is y?

InstructionCode
MOV AX, [adr]A1 + adr
SUB AX, [adr]2B 06 + adr
ADD AX, [adr]03 06 + adr
MOV [adr], AXA3 + adr
Show answer
MOV AX, [0200h]   ; A1 00 02
SUB AX, [0202h]   ; 2B 06 02 02
ADD AX, [0204h]   ; 03 06 04 02
MOV [0206h], AX   ; A3 06 02

Address bytes are written little-endian: 0202h → 02 02, 0204h → 04 02. The code is 3 + 4 + 4 + 3 = 14 bytes in total.

y = 50 − 8 + 3 = 45 (002Dh). Each assembly line corresponds to a single machine instruction; the C line leaves it to the compiler to decide which register to use.

Multiple Choice · Machine Language

Question 3: Machine Language

Which statement about machine language is correct?

D: Machine language is the binary instruction codes that the processor's control unit can decode directly: an opcode and operand fields. Every processor family has its own instruction set; on the 8086, B0 05 means "load 5 into AL," while on ARM the same job is encoded as a different 32-bit word. Hexadecimal is only for human readers; what sits in memory is bits.

Classic Question · Machine Language

Question 4: Encode and Decode

Sample machine language: 8-bit instruction = 4-bit opcode | 2-bit destination | 2-bit source; HALT 0000, ADD 0001, SUB 0010, AND 0011, OR 0100; R0 = 00 … R3 = 11; dest ← dest op src.

  1. AND R1, R3 and OR R2, R0: encode both in binary and hexadecimal.
  2. Decode the bytes 1Eh and 2Dh.
  3. If R2 = 7 and R3 = 4, what is R3 after 1Eh?
Show answer
  1. AND R1, R3 = 0011 01 11 = 37h; OR R2, R0 = 0100 10 00 = 48h.
  2. 1Eh = 0001 11 10 → ADD R3, R2; 2Dh = 0010 11 01 → SUB R3, R1.
  3. R3 ← R3 + R2 = 4 + 7 = 11. The source R2 is unchanged.

In hexadecimal, the first digit is the opcode; the second digit carries the destination and source fields together.

Step-by-Step Trace · Machine Language

Question 5: Tracing a Machine Program

A program written in the sample machine language (4-bit opcode | 2-bit destination | 2-bit source) runs from address 0. Initially R0 = 6, R1 = 9, R2 = 12, R3 = 5. What are the registers at HALT?

R06R19R212R35PC0

Start: R0 = 6, R1 = 9, R2 = 12 (1100b), R3 = 5 (0101b). PC = 0.

R06R13R212R35PC10010SUB01R100R0= 24h

24h = 0010 01 00 → SUB R1, R0: R1 = 9 − 6 = 3.

R06R13R213R35PC20100OR10R211R3= 4Bh

4Bh = 0100 10 11 → OR R2, R3: 1100 OR 0101 = 1101 → R2 = 13.

R019R13R213R35PC30001ADD00R010R2= 12h

12h = 0001 00 10 → ADD R0, R2: R0 = 6 + 13 = 19. The new value of R2 is used.

R019R13R213R31PC40011AND11R301R1= 3Dh

3Dh = 0011 11 01 → AND R3, R1: 0101 AND 0011 = 0001 → R3 = 1.

R019R13R213R31PC50000HALT00R000R0= 00h

00h → HALT. Result: R0 = 19, R1 = 3, R2 = 13, R3 = 1.

0: 0010 0100  ; 24h
1: 0100 1011  ; 4Bh
2: 0001 0010  ; 12h
3: 0011 1101  ; 3Dh
4: 0000 0000  ; 00h

Multiple Choice · Machine Language

Question 6: Eight-Register Format

In the sample machine language, an instruction consists of a 4-bit opcode and two 2-bit register fields. If the opcode field is kept and the number of registers is raised to 8, how many bits is an instruction?

A: Choosing one of 8 registers takes log2 8 = 3 bits; two register fields are 3 + 3 = 6 bits, and with the opcode 4 + 6 = 10 bits. If the instruction is to stay at 8 bits, the opcode shrinks to 2 bits and only 4 operations can be defined.

Multiple Choice · Assembly Languages

Question 7: From Program to Memory

In the toolchain from source code to a running program, which pairing is correct?

C: The assembler translates each assembly line to machine code and produces an object file; the linker combines object files with library code into an executable; the loader places this file in main memory, adjusts the necessary addresses, and hands control to the start of the program. The preprocessor only expands #include and #define lines; type checking is done by the compiler.

Output Question · Assembly Languages

Question 8: Address of the String

For the .COM program assembled in emu8086:

org 100h
mov ah, 09h
mov dx, offset msg
int 21h
mov ah, 4Ch
int 21h
msg db 'Giresun 2026!', 0Dh, 0Ah, '$'
len equ $ - msg
  1. msg sits at which relative offset?
  2. len has what value?
  3. What is printed on the screen?
  4. What would happen if the trailing '$' were forgotten?
Show answer
AddressBytesLine
0100h2mov ah, 09h
0102h3mov dx, offset msg
0105h2int 21h
0107h2mov ah, 4Ch
0109h2int 21h
010Bh16msg db …
  1. The code is 11 bytes: msg = 010Bh.
  2. 13 characters + 0Dh + 0Ah + '$' = 16. EQU takes no space.
  3. Giresun 2026! followed by a carriage return + line feed; '$' is not printed.
  4. DOS function 09h would keep printing random bytes from memory until it found a '$'.

Multiple Choice · Assembly Languages

Question 9: AT&T Syntax

Which is the Intel-syntax equivalent of the AT&T-syntax (GNU as) instruction subl %ebx, %eax?

A: In AT&T the order is source, destination; in Intel it is destination, source. Registers are written with %, and the suffix l means a 32-bit operation. A memory operand would be written (%ebx) in AT&T. The operation does not change: the result is written to the destination, that is, EAX.

Step-by-Step Trace · Assembly Languages

Question 10: Writing Characters with DOS

What does the program print? What are CX and DL after each LOOP pass?

CX0003hDL41h 'A'screen INT 21h, AH = 02h → write the character in DLLOOP: CX ← CX − 1; if CX ≠ 0 jump to again

CX = 3 passes, DL = 'A' (41h).

CX0002hDL43h 'C'screenAINT 21h, AH = 02h → write the character in DLLOOP: CX ← CX − 1; if CX ≠ 0 jump to again

Pass 1: AH = 02h, INT 21h writes 'A'. DL = 41h + 2 = 43h ('C'). LOOP: CX = 2 ≠ 0 → jump.

CX0001hDL45h 'E'screenACINT 21h, AH = 02h → write the character in DLLOOP: CX ← CX − 1; if CX ≠ 0 jump to again

Pass 2: 'C' is written; DL = 45h ('E'), CX = 1 → jump.

CX0000hDL47h 'G'screenACEINT 21h, AH = 02h → write the character in DLLOOP: CX ← CX − 1; if CX ≠ 0 jump to again

Pass 3: 'E' is written; DL = 47h ('G'), CX = 0 → no jump.

CX0000hDL47h 'G'screenACEINT 21h, AH = 02h → write the character in DLLOOP: CX ← CX − 1; if CX ≠ 0 jump to again

RET: the .COM program returns to DOS. The screen shows ACE; the last DL ('G') was never written.

org 100h
mov cx, 3
mov dl, 'A'
again: mov ah, 02h
int 21h
add dl, 2
loop again
ret

Multiple Choice · Assembly Languages

Question 11: System Call

On 32-bit Linux (x86), where is the service number placed to write to the screen, and how is the kernel called?

D: Linux x86 (32-bit): the service number goes in EAX (4 = write, 1 = exit), the parameters in EBX, ECX, EDX, and the transition is int 0x80. The others are also correct but for other environments: AH = 09h + INT 21h is MS-DOS, r7 + svc #0 is ARM Linux, RAX = 1 + syscall is 64-bit Linux.

Classic Question · Assembly Languages

Question 12: stdcall and ESP

On 32-bit Windows, a three-parameter stdcall function is called while ESP = 0012FF70h:

push 1      ; c
push 2      ; b
push 3      ; a
call Func   ; Func(a, b, c)
  1. What is ESP at the moment the function is entered? What do [ESP] and [ESP+4] hold?
  2. When the function returns with ret 12, what is ESP?
  3. Why are the parameters pushed in the order c, b, a?
Show answer
  1. Each PUSH decreases ESP by 4: 3 parameters + the return address = 16 bytes → ESP = 0012FF60h. [ESP] is the return address, [ESP+4] is the first parameter, a = 3.
  2. ret 12 pops the return address (+4) and discards the 12 bytes of parameters → ESP = 0012FF70h, the value before the call. In stdcall the callee cleans up the stack.
  3. When pushed right to left, the first parameter lands at the top of the stack, just above the return address; the function always finds it in the same place.

Multiple Choice · High-Level Languages

Question 13: How Does Java Run?

Which statement about how a Java program runs is correct?

B: Java is a hybrid: javac Main.java produces a platform-independent .class file (bytecode); running java Main, the JVM first interprets this code and compiles frequently executed parts to machine code with a JIT. That is why the same .class file runs on every platform that has a JVM. C# is likewise compiled to IL intermediate code.

Step-by-Step Trace · High-Level Languages

Question 14: for and if on the 8086

The C code for (i = 1; i <= 4; i++) if (i % 2 == 0) sum += i; — in its 8086 equivalent, what are CX and AX at the end of each pass? What is AX when the program finishes?

CX (i)0001hAX (sum)0000hZF1CF0C: for (i = 1; i <= 4; i++)

i = 1, sum = 0. xor ax, ax clears AX and sets ZF = 1.

CX (i)0002hAX (sum)0000hZF0CF0C: if (i % 2 == 0) sum += i;

i = 1: 1 − 4 needs a borrow (CF = 1) → JA does not jump. 1 AND 1 ≠ 0 → odd, not added to the sum. i = 2.

CX (i)0003hAX (sum)0002hZF0CF0C: if (i % 2 == 0) sum += i;

i = 2: 2 AND 1 = 0 → ZF = 1, JNZ does not jump → sum = 0 + 2 = 2. i = 3.

CX (i)0004hAX (sum)0002hZF0CF0C: if (i % 2 == 0) sum += i;

i = 3: odd → skipped. i = 4.

CX (i)0005hAX (sum)0006hZF0CF0C: if (i % 2 == 0) sum += i;

i = 4: CMP 4, 4 → ZF = 1, CF = 0; JA jumps only when CF = 0 and ZF = 0 → continue. Even → sum = 6. i = 5.

CX (i)0005hAX (sum)0006hZF0CF0exit: 5 > 4

CMP 5, 4: CF = 0, ZF = 0 → JA jumps to done. Result: AX = 6 = 2 + 4.

org 100h
mov cx, 1        ; i = 1
xor ax, ax       ; sum = 0
again: cmp cx, 4
ja done         ; finish if i > n
test cl, 1       ; lowest bit
jnz skip         ; if odd, do not add
add ax, cx
skip: inc cx
jmp again
done: ret

Output Question · High-Level Languages

Question 15: Range in Python

The C code for (int i = 1; i <= n; i++) has been translated to Python as shown below. What is the output? Is it the same as the output of the C version?

n = 4
for i in range(1, n):
    if i % 2 == 0:
        print(f"{i} even")
    else:
        print(f"{i} odd")
Show answer
1 odd
2 even
3 odd

No. range(1, n) does not include the upper bound: i = 1, 2, 3. The C version also prints the line "4 even". The correct form is range(1, n + 1).

Since Python is an interpreted language, this error is not caught at compile time but only when the output is examined; syntactically the program is correct.

Short Answer · High-Level Languages

Question 16: Compilation Stages

gcc oddeven.c -o oddeven runs four tools back to back. For each stage give the tool, the output file, and the gcc option used to stop at that stage. Which fifth component comes into play when the program is run?

Show answer
StageOutputOption
Preprocessor (#include, #define)oddeven.i-E
Compiler (C → assembly)oddeven.s-S
Assembler (assembly → machine code)oddeven.o-c
Linker (+ printf, scanf …)oddeven(.exe)—

The fifth component is the loader: the operating system places the program in memory and jumps to main. To see the intermediate assembly in Intel syntax, use gcc -S -masm=intel.

The End

Chapter 3.1: Programming Languages: Study Questions
Lecture slides: Chapter 3.1: Programming Languages · Lecture notes
1 / 1 Sercan KÜLCÜ. All rights reserved.