לדלג לתוכן

1.4 - המחסנית - פתרון

תרגיל 1 – PUSH / POP,‏ PUSHA / POPA

stack_ex1.asm

IDEAL
MODEL small
STACK 100h

DATASEG
; no constant data

CODESEG
start:
    mov ax, @data
    mov ds, ax

    ; ----- Part A: PUSH / POP -----
    mov ax, 1111h
    mov bx, 2222h
    push ax
    push bx

    pop cx        ; CX = 2222h
    pop dx        ; DX = 1111h

    ; Note: if we wrote pop ax twice in a row here
    ;‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑‑
    ; the CPU would pull "garbage" off the Stack (Underflow).

    ; ----- Part B: PUSHA / POPA -----
    pusha         ; pushes AX CX DX BX SP BP SI DI (16 bytes)
    ; ... registers can be changed as we wish ...
    popa          ; restores all of them

exit:
    mov ax, 4C00h
    int 21h
END start

תרגיל 2 – CALL ו‑RET

stack_ex2.asm

IDEAL
MODEL small
STACK 100h

CODESEG
start:
    ; the jump address will return here
    call HelloFunc
    mov ax, 7777h     ; execution continues after the return

    ; at this point the Stack is clean - the return address was already popped by RET

exit:
    mov ax, 4C00h
    int 21h

; empty function
HelloFunc:
    ret
END start


תרגיל 3 – פרמטרים באמצעות המחסנית

stack_ex3.asm

IDEAL
MODEL small
STACK 100h

CODESEG
start:
    ; --- Square(n) : n² ---
    mov ax, 4
    push ax
    call Square          ; AX = 16

    ; --- AddTwo(a,b) : a+b  (cdecl) ---
    push 2               ; param 2 (b)
    push 5               ; param 1 (a)
    call AddTwo
    add sp, 4            ; caller cleans (cdecl).  AX = 7

    ; --- AddTwoPascal(a,b) : a+b  (pascal) ---
    push 5               ; param 1
    push 3               ; param 2
    call AddTwoPascal    ; callee cleans.  AX = 8

exit:
    mov ax, 4C00h
    int 21h

; ---------- Functions ----------

Square:                  ; [BP+4] = n
    push bp
    mov  bp, sp
    mov  ax, [bp+4]
    mul  ax              ; AX = AX*AX
    pop  bp
    ret

AddTwo:                  ; cdecl – caller cleans
    push bp
    mov  bp, sp
    mov  ax, [bp+4]
    add  ax, [bp+6]
    pop  bp
    ret

AddTwoPascal:            ; pascal – callee cleans
    push bp
    mov  bp, sp
    mov  ax, [bp+4]
    add  ax, [bp+6]
    pop  bp
    ret 4                ; also cleans the 2 parameters
END start


תרגיל 4 – משתנים לוקליים עם BP

stack_ex4.asm

IDEAL
MODEL small
STACK 100h

CODESEG
start:
    call LocalVarsFunc   ; result returns in AX
exit:
    mov ax, 4C00h
    int 21h

; -------------------------------
; reserves 4 bytes: var1=[BP-2], var2=[BP-4]
; returns AX = var1^2 + var2
LocalVarsFunc:
    push bp
    mov  bp, sp
    sub  sp, 4

    mov  word ptr [bp-2], 3   ; var1 = 3
    mov  word ptr [bp-4], 7   ; var2 = 7

    mov  ax, [bp-2]
    mul  ax                   ; AX = var1²
    add  ax, [bp-4]           ; AX += var2

    mov  sp, bp
    pop  bp
    ret
END start


תרגיל 5 – שמירת רגיסטרים

stack_ex5.asm

IDEAL
MODEL small
STACK 100h

CODESEG
start:
    call SafeCalc
exit:
    mov ax, 4C00h
    int 21h

; SafeCalc uses AX,BX and guarantees not to change them
SafeCalc:
    push bp
    mov  bp, sp
    push ax
    push bx

    mov  ax, 10
    mov  bx, 3
    add  ax, bx             ; just a temporary calculation

    pop  bx
    pop  ax
    pop  bp
    ret
END start


תרגיל 6 – פונקציה מלאה (Multiply)

stack_ex6.asm

IDEAL
MODEL small
STACK 100h

DATASEG
Product dw ?

CODESEG
start:
    push 3
    push 5
    call Multiply   ; AX = 15
    add sp, 4
    mov Product, ax
exit:
    mov ax, 4C00h
    int 21h

Multiply:                  ; cdecl  AX = a*b
    push bp
    mov  bp, sp
    mov  ax, [bp+4]
    mov  bx, [bp+6]
    mul  bx                ; DX:AX = AX*BX  -> AX is the low result
    pop  bp
    ret
END start


תרגיל 7 – cdecl לעומת pascal

stack_ex7.asm

IDEAL
MODEL small
STACK 100h

CODESEG
start:
    ; --- cdecl ---
    push 4
    push 6
    call MulCdecl
    add sp, 4          ; caller cleans

    ; --- pascal ---
    push 6
    push 4
    call MulPascal     ; callee cleans

exit:
    mov ax, 4C00h
    int 21h

; ---------- Functions ----------

MulCdecl:                  ; AX = a*b   (caller cleanup)
    push bp
    mov  bp, sp
    mov  ax, [bp+4]
    mov  bx, [bp+6]
    mul  bx
    pop  bp
    ret

MulPascal:                 ; AX = a*b   (RET 4)
    push bp
    mov  bp, sp
    mov  ax, [bp+4]
    mov  bx, [bp+6]
    mul  bx
    pop  bp
    ret 4
END start


תרגיל 8 – אתגר

stack_ex8.asm

IDEAL
MODEL small
STACK 100h

DATASEG
ExprResult   dw ?    ; a + b*c
TempResult   dw ?    ; temporary result

CODESEG
start:
    ; --------  a + b*c  --------
    push 3      ; c
    push 2      ; b
    push 1      ; a
    call APlusBtimesC       ; AX = 1 + 2*3 = 7
    add sp, 6
    mov ExprResult, ax

    ; --------  function with local tmp --------
    push 9
    call WithTemp
    add sp, 2
    mov TempResult, ax

exit:
    mov ax, 4C00h
    int 21h

; a + b*c   (cdecl)
APlusBtimesC:
    push bp
    mov  bp, sp
    mov  ax, [bp+6]     ; b
    mul  word ptr [bp+4] ; b * c   (c is at [bp+4])
    add  ax, [bp+8]     ; + a      (a is at [bp+8])
    pop  bp
    ret

; WithTemp(n): returns (n+1)*2 with a local variable
WithTemp:
    push bp
    mov  bp, sp
    sub  sp, 2          ; tmp  = [bp‑2]

    mov  ax, [bp+4]
    inc  ax
    mov  [bp-2], ax     ; tmp = n+1
    add  ax, ax         ; (n+1)*2  → AX

    mov  sp, bp
    pop  bp
    ret
END start