0

Hi I have already written a program in Assembly 64 bit mode in connection with C, that counts the number of left and right brackets check here:

bits 64
    section .data
    extern  g_left, g_right, g_str
        section .text
    global count
count:
    enter 0,0
    mov eax, 0                      
    mov ebx, 0                          
    mov ecx, 0              
.back:
    cmp [g_str + eax], byte 0       
    je .out
    cmp [g_str + eax], byte '['
    jne .right
    inc ebx
.right:
    cmp [g_str + eax], byte ']'
    jne .skip
    inc ecx
.skip:
    inc eax
    jmp .back
.out:
    mov [g_l], ebx              
    mov [g_r], ecx                  
    leave
    ret

C code:

#include <stdio.h>
void count();
char g_str[] = "[[[]]]][[32423]][234dsfsdf";
int g_left, g_right;
int main()
{
    count();
    printf("left = %d and right = %d\n", g_left, g_right);
}

What I want is to use this assembly code but change it a bit so that a function that is called in C with a string as input and just prints the number of brackets. Also, I want it in 32-bit mode this time. It should look like this:

int brackets( char *t_str );

I'm new to assembly and confused on how to change my code, please help me.

1201ProgramAlarm
  • 32,384
  • 7
  • 42
  • 56
Kolop
  • 9
  • 2
  • I have no clue what you mean, maybe tell me where to go? – Kolop Mar 25 '21 at 14:17
  • See https://stackoverflow.com/questions/61341/is-there-a-way-to-insert-assembly-code-into-c – LBald Mar 25 '21 at 14:18
  • Does this answer your question? [Is there a way to insert assembly code into C?](https://stackoverflow.com/questions/61341/is-there-a-way-to-insert-assembly-code-into-c) – LBald Mar 25 '21 at 14:22
  • I think I wasn't clear enough, I want to use assembly code to calculate the number of brackets but call it with a function from C, but in my code I use variables, I want to just pass the string from the C function into assembly and caclulate – Kolop Mar 25 '21 at 14:25
  • Note that your code looks a lot like 32 bit code. Are you sure you want to write a 64 bit program, not a 32 bit program? – fuz Mar 25 '21 at 14:34
  • I want to write a 32 bit mode program – Kolop Mar 25 '21 at 14:35
  • @Kolop Then why does your program start with `bits 64`? It already is a 32 bit program. – fuz Mar 25 '21 at 14:38
  • But it does start with bits 64 check again ;) – Kolop Mar 25 '21 at 14:40
  • @Kolop I know that. And that is confusing me because the code is 32 bit code and will not work correctly in 64 bit mode. You cannot just take 32 bit code, stick `bits 64` in front and have a 64 bit program. It doesn't work like that. – fuz Mar 25 '21 at 14:41
  • Yeah well sorry I'm pretty new so still learning, teacher didn't have a problem with the code so I guessed it was good – Kolop Mar 25 '21 at 14:45
  • Your code example is wrong. You're declaring the variables `g_left` and `g_right` but your assembly function uses the names `g_l` and `g_r`. – ecm Mar 25 '21 at 15:26
  • 1
    @fuz: Because of NASM's `default abs` setting by default it may be that the `bits 64` just does nothing, effectively. (Uses the long encoding of disp32-only addresses with SIB byte.) There's no stack operations or frame offsets that depend on the bitness. The `mov` with immediates default to 32-bit immediates in `bits 64` too. Though considering my previous message perhaps the code wasn't run at all. – ecm Mar 25 '21 at 15:32
  • 2
    @ecm Quite honestly, I doubt OP wrote it. – fuz Mar 25 '21 at 15:56
  • It seems that you need to do some research about ABI and how arguments and return values are exchanged between callers and callee. This question is too braod to be answered here, and I'm sure, since this seems to be some kind of homework, that you have all bits in your lectures. – the busybee Mar 25 '21 at 16:00
  • That already is 32-bit code, or at least it *should* be assembled as 32-bit; not the use of 32-bit registers in addressing modes! – Peter Cordes Mar 25 '21 at 17:15
  • 1
    @ecm: Almost, but not quite: Under `bits 64`, `cmp [g_str + eax], byte 0` will use an address-size prefix. When decoded in 32-bit mode, that prefix will set the address size to 16-bit and break everything! (It's even length-changing for a [reg+disp32] addressing mode, so it will decode some address bytes as part of the another instruction.) It's plausible the OP is assembling and linking into a 64-bit executable, except for the `g_l` / `g_r` undefined symbols which yeah hint that it hasn't actually been run at all in this form. – Peter Cordes Mar 25 '21 at 17:20

0 Answers0