# Thread: Mixed Language: C and Asm

Z 90 = 0b 0101 1010
z 122 = 0b 0010 0000

0b 0101 10101 | 0x20 would give me something else, not 122.
EDIT:
i think i made mistake in the calculation. brb

2. 90 is 0b01011010, true. 122, however, is 0b01111010, not 0b00100000 which is just 32.

3. Yeah it worked.
I am going to try the reverse, convert from lower to upper. I don't understand why we AND, and not OR.

4. I just did it. And I just had to do
a&~0x20...where did 0xdf come from, it works but why not use ~0x20? :S

5. Originally Posted by Eman
I just did it. And I just had to do
a&~0x20...where did 0xdf come from, it works but why not use ~0x20? :S
If you can spot the difference between ~0x20 and 0xdf, then you're a better man than I Gunga Din.

As to why the difference between and and or, think about what needs to happen for five seconds.

6. ~0x20 is a negative number -224
0xdf is a positive 223.
btw, who is Gunga Din?

AND is used to clear, while OR was used to set? (all this binary...)

7. Originally Posted by Eman
~0x20 is a negative number -224
0xdf is a positive 223.
Um, no. ~0x20 = 0b 1101 1111, while 0xdf is 0b 1101 1111. (However, if I'm not mistaken, you can type 0xdf in an assembler program, while you can't type ~0x20. I might be wrong there though.)
Originally Posted by Eman
btw, who is Gunga Din?
Gunga Din - Wikipedia, the free encyclopedia

Originally Posted by Eman
AND is used to clear, while OR was used to set? (all this binary...)
So, you do know what and means, right? And you do know what or means, right?

8. Originally Posted by tabstop
Um, no. ~0x20 = 0b 1101 1111, while 0xdf is 0b 1101 1111. (However, if I'm not mistaken, you can type 0xdf in an assembler program, while you can't type ~0x20. I might be wrong there though.)
i should really use a calculator.

So, you do know what and means, right? And you do know what or means, right?
em, what i have said? lol i don't know what you want me to see

9. Originally Posted by Eman
i should really use a calculator.

em, what i have said? lol i don't know what you want me to see
"and" means "both have to be true (or 1)". "or" means "only one has to be true (or 1)". So you should be able, at some point, to see the difference between 0b 0101 1001 AND 0b 0010 0000, and 0b 0101 1001 OR 0b 0010 0000. And the same for working with 0b 1101 1111.

10. Originally Posted by tabstop
"and" means "both have to be true (or 1)". "or" means "only one has to be true (or 1)". So you should be able, at some point, to see the difference between 0b 0101 1001 AND 0b 0010 0000, and 0b 0101 1001 OR 0b 0010 0000. And the same for working with 0b 1101 1111.
Code:
```  0101 1001
&0010 0000
0000  0000==0

0101 1001
| 0010 0000
0111 1001==121

0101 1001
1101 1111
& 0101 1001==89  gives me original number

0101 1001
|  1101 1111
1101 1111==0xdf gives me the mask```
There is a difference alright. But what are you trying to tell me?

11. Originally Posted by Eman
Code:
```  0101 1001
&0010 0000
0000  0000==0

0101 1001
| 0010 0000
0111 1001==121

0101 1001
1101 1111
& 0101 1001==89  gives me original number

0101 1001
|  1101 1111
1101 1111==0xdf gives me the mask```
There is a difference alright. But what are you trying to tell me?
You asked why we use and in some circumstances, and or in others. I am attempting (and apparently failing) to show you the difference between them so that you know why we use and sometimes, and or other times.

12. tbh from that. All I can say is you could use the bitwise & to check if every bit is 0. Or to retain the original bits.
And the OR is used to set individual bits.

I wrote this code, and i have been getting seg fault. Can you help me spot what is wrong.I have been at it for half an hr. Ty
Code:
```
#include <stdio.h>

void cstring(char *s, char *d) ;
int main()
{
char source[]="Hello World" ;

char dest[12] ;

printf("%s\n", source) ;

cstring(source, dest) ;

//printf("After function call: \n") ;
//  printf("%s\n", dest) ;

//fputs(source, stdout) ;

return 0 ;
}```
Code:
```.section .data

.section .text

.global cstring

cstring:
push %ebp
mov %esp, %ebp

push %esi
push %edi
push %eax
mov 12(%ebp),%esi  #esi to point to source

mov 8(%ebp),%edi   # edi to point to dest

copyLoop:
cmpb \$0, (%esi)
je exit

movb (%esi),%cl
movb %cl, (%edi)

inc %esi
inc %edi

jmp copyLoop

exit:
mov %ebp, %esp
pop %eax
pop %edi
pop %esi
pop %ebp

ret
.end```

13. Since arguments are pushed right-to-left, the one on the "top" of the stack (nearest ebp) is the first argument.

14. so it should be
movl 8(%ebp),%esi to point to source.
I tried that, i still got the same error

15. Also:
Code:
`mov %ebp, %esp`
means "SAY GOODBYE TO THE STACK!!!!!" The whole purpose of the pops is to bring esp down while moving the clobbered registers back to what they're supposed to be. If you fiddle with esp, now you're popping the old ebp into eax, the eip (i.e., where to return to from this function) into edi, probably the [11] byte of one of your strings (depending which one was loaded where) into esi, the [10] byte of that string into ebp [eep!] and then using the [9] byte of the string as the return address to jump to with ret. And that's not going to be pleasant.