Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
en:multiasm:exercisebook:pc:sut:scenarios_standalone [2026/05/20 15:11] – [Implementation of calculation functions] ktokarzen:multiasm:exercisebook:pc:sut:scenarios_standalone [2026/05/20 15:57] (current) – [Implementation of calculation functions] ktokarz
Line 186: Line 186:
  
 <figure ex_stack_simple> <figure ex_stack_simple>
-{{ :en:multiasm:exercisebook:pc:ex_stack_simple.png?400 |Stack view inside the funtion with 6 arguments}} +{{ :en:multiasm:exercisebook:pc:ex_stack_simple.png?400 |Stack view inside the function with 6 arguments}} 
-<caption>Stack view inside the funtion with 6 arguments</caption>+<caption>Stack view inside the function with 6 arguments</caption>
 </figure> </figure>
  
  
-The caller passes arguments according to the Windows x64 ABI. First four arguments through RCXRDX, R8 and R9 registers. Further arguments are placed onto the stack. The responsibility of the caller is also to reserve the shadow space for all arguments before the call. That's why there are 32 bytes reserved before the return address is automatically placed onto the stack by the call instruction.+The caller passes arguments according to the Windows x64 ABI. First four arguments through RCXRDX, R8 and R9 registers. Further arguments are placed onto the stack. The caller is also responsible for reserving the shadow space for all arguments before the call, even those passed through registers. That's why 32 bytes are reserved before the return address is automatically placed on the stack by the call instruction. 
 + 
 +Please note the order of arguments. It is assumed that they are placed onto the stack in reverse order. The last argument is placed on the stack first. That's why the 6th argument is at the higher address, next is the 5th argument and next there is a shadow space for arguments 1 - 4. From the perspective of a function, the first argument (or rather its shadow) is just after the return address. As the return address consumes 8 bytes, the shadow space for the first argument is at address SP+8. 
 + 
 +How to call such a function? Putting the first four parameters into registers is quite simple. To place remaining arguments onto the stack, it is possible to use the **push** instruction. 
 +<code asm> 
 +;call sum of 6 integers function 
 +    
 +    mov rcx, 1     ; 1st argmument 
 +    mov rdx, 2     ; 2nd argmument 
 +    mov r8, 3      ; 3rd argmument 
 +    mov r9, 4      ; 4th argmument 
 +    mov r11, 6 
 +    push r11       ; 6th argument 
 +    mov r10, 5 
 +    push r10       ; 5th argument 
 +     
 +    sub rsp, 20h   ; 32 bytes of the shadow space 
 +     
 +    call sum_6_int ; function call 
 +     
 +    add rsp, 30h   ; stack cleanup 
 +    mov rcx, rax   ; result in rax 
 +</code> 
 + 
 +The figure {{ref>ex_stack_caller_push}} shows the stack organisation from the caller's perspective. First, the 6th argument is pushed onto the stack. Next, the 5th argument is pushed. Next, the 32 bytes of the shadow space are reserved with the subtraction instruction **sub rsp, 20h**. Finally, the return address is pushed by the **call** instruction. The arrows point to the addresses (where RSP points) after the specified instructions. 
 +<figure ex_stack_caller_push> 
 +{{ :en:multiasm:exercisebook:pc:ex_stack_caller_push.png?400 |Stack view from caller function}} 
 +<caption>Stack view from caller function</caption> 
 +</figure> 
en/multiasm/exercisebook/pc/sut/scenarios_standalone.1779279066.txt.gz · Last modified: by ktokarz
CC Attribution-Share Alike 4.0 International
www.chimeric.de Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0