Dont know if I did it right

This is a variation of the program shown in Figure 6.12. The change from the original is noted below. Translate this C++ program to assembly language and upload your .pep file here. Include comments and format and symbol trace tags as appropriate in your program.

#include <iostream>
using namespace std;

int main() {
int cop = 0; // Local rather than global variables
int driver = 40;
do{
cop += 25;
driver += 20;
}
while (cop < driver);
cout << cop;
return 0;
}

Code I have:

main:
add $t1,$t1,0 #$t1-cop
add $t2,$t2,40 #$t2-driver
j overtake
overtake:
do:
add $t1,$t1,25
add $t2,$t2,20
blt $t1,$t2,do
mov $a0,$t1
li $v0, 1
syscall
li $v0, 10
syscall

Looks a lot like mips

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
.text                    # assembler directive to indicate start of program
main:
    addi $t1,$zero,0     # alternative -> li $t1, 0
    addi $t2,$zero,40    # alternative -> li $t2, 40
    # j overtake
do: bge $t1, $t2, exit   # if the contents of register $t1 is greater than or equal to register $t2, exit
    addi $t1,$t1,25
    addi $t2,$t2,20
    b do
exit:
    move $a0, $t1        # copy contents of register $t1 to register $a0 for printing
    li $v0, 1            # service code to print a number
    syscall
    li $v0, 10           # service code to exit the program
    syscall


Note you use addi to add constants to contents of registers, add is used to add contents of registers to each other. This is true for any other command that has the i appended to the end of it
Last edited on
Oh I see thanks for teaching me that!
Topic archived. No new replies allowed.