Programs become more and more interesting when you have dynamic elements in them. On such way of bringing your program to life is by adding logic. In assembly, the task can seem dubious and awkward, but once you get a grip on the concept, it will be but second nature.
So lets get started!
To write this in assembly, consider the following:
Here, all we did is create our variable x, and the respective message that we will display depending on the result of our if statement.
Continually:
So lets get started!
//We want to write an equivalent program to this in assembly#include <stdio.h>int main(){int x = 40;if( x > 10){printf("x is greater than 10\n");}else{printf("x is lesser than 10\n");}return 0;}
To write this in assembly, consider the following:
;an equivalent program to this in assembly
SECTION .data
x: dd 40
msg1: db "x is greater than 10", 10, 0
msg2: db "x is lesser than 10", 10, 0
SECTION .text
Here, all we did is create our variable x, and the respective message that we will display depending on the result of our if statement.
Continually:
;an equivalent program to this in assembly
SECTION .data
x: dd 40
msg1: db "x is greater than 10", 10, 0
msg2: db "x is lesser than 10", 10, 0
SECTION .text
;allow access to printf
extern printf
;make our main available externally
global main
main:
push ebp
mov ebp , esp
cmp DWORD [x] , 10
jg .conditionIsTrue ;translates to: jmp if [x] > 10
.conditionNotTrue: ;translate to: else
push DWORD msg2
call printf
.conditionIsTrue:
push DWORD msg1
call printf
jmp .done
.done:
mov esp, ebp
pop ebp
ret
Dont you need jump to .done at the end of conditionNotTrue
ReplyDeleteKeep it up!
ReplyDelete