Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ZCPU HL-ZASM C structs don't work correctly #69

Open
EnoSkyFire opened this issue Sep 22, 2024 · 1 comment
Open

ZCPU HL-ZASM C structs don't work correctly #69

EnoSkyFire opened this issue Sep 22, 2024 · 1 comment

Comments

@EnoSkyFire
Copy link

EnoSkyFire commented Sep 22, 2024

1. The Problem

The current structs that are implemented do not work correctly. There are two issues. First issue is the struct returned in a function is not correct. Secondly, when I use #pragma CRT ZCRT with void main(){}, my returned struct gives an address value.

2. First issue - returning structs

DerelictDrone posted this image with working code were 2D vector addition is executed but in the result the register R2 should be 4 and not 3. I tested this code and I get the same result. The issue lies with returning the struct since outputting x and y within the function itself gives the correct values. source: (#17)
problem

3. Second issue - using void main() {}
Furthermore, when I try using structs with #pragma CRT ZCRT and with void main(){} instead of the main label, the returned struct has an address value. See code and result below.

Code:

//#pragma set OutputFinalListing true
#pragma CRT ZCRT

//jmp main

struct vec2 {
    float x;
    float y;
};

vec2 addvec(vec2 v, vec2 v2) {
    v.x += v2.x;
    v.y += v2.y;
    return v;    
    
}

vec2 v;
vec2 v2;

//main:
void main() {
    v.x = 1;
    v.y = 2;
    v2.x = 3;
    v2.y = 4;
    
    MOV R0,v.x
    MOV R1,v.y
    OUT 0, R0
    OUT 1, R1
    
    v = addvec(v,v2);
    
    MOV R2,v.x
    MOV R3,v.y
    OUT 2, R2
    OUT 3, R3
    
    MOV R4,v2.x
    MOV R5,v2.y
    OUT 4, R4
    OUT 5, R5
    
    v2 = v
    
    MOV R6,v2.x
    MOV R7,v2.y
    OUT 6, R6
    OUT 7, R7
}

Result:
image

@WallopingEwe
Copy link
Contributor

WallopingEwe commented Dec 11, 2024

When you pass a struct via a function you need to pass a pointer for it. Also in your code your return v is just returning v's position in the stack and not a new struct which would explain R2 being at the end of your memory.

void addvec(vec2* v, vec2* v2) {
    v.x += v2.x;
    v.y += v2.y;
}
addvec(&v, &v2);

This will allow addvec to index them correctly and v will have v2 added to it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants