Search code examples
pointersassemblymov

Basic assembly in mov function pointers


I want to put a new value into a pointer (in the value that he point of him) but I fail.

The program push to the stack

offset result, num1 and num2

The maximize need to be in result...

Now I need your help

org  100h

jmp main

    result      dw ?
    num0        dw ?
    num1        dw ?


main:  
    mov bp,sp     
    push (offset result) 
    push num0 
    push num1
    call max
    MOV AX,num0
    call print_num
    PRINTN
    MOV AX,num1
    call print_num
    PRINTN
    MOV AX,result
    call print_num
    PRINTN 
    ret

max PROC

    push bp        
    mov bp,sp
    MOV AX,[BP+6]
    MOV BX,[BP+4]
    MOV CX,[BP+2]
    CMP AX,BX
    JGE BIG
    MOV [CX],BX 
BIG:
    MOV [CX],AX  
    mov sp,bp
    pop bp
    ret

max ENDP


include magshimim.inc \\our program built for us defines for the inputs...

I want to do:

MOV [CX] , AX

but the emu8086 doesn't really like me :)

Thanks for your help!


Solution

  • Problems found:

    • "push (offset result)" seems to store the wrong value because of the parenthesis.
    • As soon as procedure "max" begins, BP is pushed on stack, so the parameters are no longer in BP+6, +4 and +2, they are in BP+8, +6 and +4.
    • "[CX]" can't be used as pointer, let's change it by BX.
    • It is necessary to skip label "BIG" in case CX is greater.

    Here is the code with the little fixes :

    org  100h
    
    jmp main
    
        result      dw ?
        num0        dw 5    ;VALUE TO TEST.
        num1        dw 2    ;VALUE TO TEST.
    
    
    main:  
        mov bp,sp     
        push offset result  ;PARENTHESIS SEEM TO STORE THE WRONG VALUE.
        push num0 
        push num1
        call max
        MOV AX,num0
        call print_num
        PRINTN
        MOV AX,num1
        call print_num
        PRINTN
        MOV AX,result
        call print_num
        PRINTN 
        ret
    
    max PROC
    
        push bp         ;"BP" IN STACK. PARAMTERS ARE NO LONGER IN BP+6,+4,+2.
        mov bp,sp
    
        MOV BX,[BP+8]   ;BX = RESULT'S ADDRESS.
        MOV AX,[BP+6]   ;AX = NUM0'S VALUE.
        MOV CX,[BP+4]   ;CX = NUM1'S VALUE.
        CMP AX,CX
        JGE BIG
        MOV [BX],CX 
        jmp finish      ;NECESSARY TO SKIP "BIG".
    BIG:
        MOV [BX],AX  
    ;    mov sp,bp
    finish:
        pop bp
        ret
    
    max ENDP