J. White J. White - 1 month ago 16
C Question

how do i simplify/condense the code (if possible)?

I'd like to present you my program in c and assembler code attached to his one. also, I've got some questions.

here is a piece of code in c

#include <stdio.h>
void podaj_znak(int tab[], int n);
int main()
{
int tab[7] = {4, 5, 6, 2, -80, 0, 56};
printf("Przed: ");
for (int i = 0; i < 7; i++)
printf("%d ", tab[i]);
printf("\n");
podaj_znak(tab, 7);
printf("Po: %d %d %d %d %d %d %d", tab[0], tab[1], tab[2], tab[3], tab[4], tab[5], tab[6]);
printf("\n");
return 0;
}


and asm right here

.686
.model flat
public _podaj_znak
.code
_podaj_znak PROC

push ebp
mov ebp, esp
mov edx, [ebp+8]
mov ecx, [ebp+12]
ptl:
mov eax, [edx]
cmp eax, 0
jl minus
ja plus
mov ebx, 0
mov [edx], ebx
jmp dalej
minus: mov ebx, -1
mov [edx], ebx
jmp dalej
plus: mov ebx, 1
mov [edx], ebx
jmp dalej

dalej: add edx, 4
sub ecx, 1
jnz ptl
pop ebp
ret
_podaj_znak ENDP
END


my question is, how can I simplify/condense the code?

edit: posting what the program does and what I like it to be like. it is just for me to train and to get used to assembler. the program is like you've got numbers from -inf to inf and it when the actual number is equal 0, it stays as it is, when it is something less than 0, it is replaced by -1, and when the number is more than 0, it is replaced by 1. the thing is, that I wanted to somehow optimize assembler code, but I don't know whether it is even possible to condense it.

Answer

It is possible to optimize a little bit the assembly by calling only one time the mov [edx], ebx as follow:

ptl:
mov eax, [edx]
cmp eax, 0
jl minus
ja plus
mov ebx, 0            ; only set to 0
jmp dalej
minus: mov ebx, -1    ; only set to -1
jmp dalej
plus: mov ebx, 1      ; only set to 1
jmp dalej

dalej: mov [edx], ebx ; update the array[edx]
add edx, 4
sub ecx, 1
jnz ptl
Comments