0

I create this piece of code in VS (C++)

#include<iostream>
using namespace std;
static short arr[10];

void powers() {
    _asm {
        mov ecx, 0;
        mov dx, 0;
        for:
        mov ax, cx;
        inc ax;
        mul ax;
        mov[arr + 2 * ecx], ax;
        inc ecx;
        cmp ecx, 10;
        jl for;
    }
}

Now I want to create another function which prints "Pow (x) = x^2", I stuck here:

void print_power(unsigned short x) {
   const char* f = "Pow(%d) = %d \n";

    _asm {
             call powers
             push f
             add esp, 4
         }
}

When I call my "powers" function from "print_power" -> my arr[10] gets filled with the ^2 of 1 to 10 (arr[0] = 1, arr[1] = 4, arr[2] = 9, arr[3] = 16, arr[4] = 25 .....) (I think)

I want when I call my print_power(x) function from main(), for example print_power(8) -> to print the 8th element from my arr like this: "Pow (8) = 81".

Thank you in advance, also I want to apologize if I have some mistakes.

cigien
  • 57,834
  • 11
  • 73
  • 112
cwva44
  • 9
  • 2
  • 2
    So what's stopping you from calling printf? Do you want to do that part in asm, too? Also, `pow` is normally a 2-arg function that takes a base and exponent. You seem to be implementing `Square(x)`, not `Pow(b, e)`. Also, 8^2 is 64, so it makes more sense to print the number that gets squared, not the array index. Or print `squares[8] = 81` to make it clear it's an array; it just looks silly to print `Pow(8) = 9^2`. – Peter Cordes Mar 24 '21 at 13:25
  • Does this answer your question? [How to output information in Visual Studio by assembler](https://stackoverflow.com/questions/58688328/how-to-output-information-in-visual-studio-by-assembler) – Peter Cordes Mar 24 '21 at 13:26
  • Yes I want printf to take place in my _asm function "print_power". I do not know which registers to push and pop and in what order, in order to do that. – cwva44 Mar 24 '21 at 13:34

0 Answers0