Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Oh yeah, it's assembly language (MIPS)
New Posts  All Forums:Forum Nav:

Oh yeah, it's assembly language (MIPS)

post #1 of 8
Thread Starter 
Ok, so I'm taking a computer architecture class and I'm just learning assemble language. I have to write a calculator that does addition, subtraction, multiplication, and division. I've got most of my code written, but the program I'm using to run it is telling me I have syntax errors about not being able to use opcodes as labels. We have the option of representing add, subtract, multiply, and divide with 1, 2, 3, and 4, or using the actual characters. Using the integers would be easier for me, but I think using the actual characters would look better so that's what I'm trying to do, but I don't think I'm doing it right. Can someone look at my code and see what's wrong? My errors are in my branch statements.
Code:
.data
pr1:    .asciiz     "Input integer>"
pr2:    .asciiz     "Input an operation (choices are +, -, *, /)>"
smsg:   .asciiz     "The sum is: "
pmsg:   .asciiz     "The product is: "
qmsg:   .asciiz     "The quotient is: "
zmsg:   .asciiz     "Cannot divide by 0"
nl:     .asciiz     "\n"
word:   .word 4

        .text
        .globl main
main:

        li      $s0, 43     #add
        li      $s1, 45     #subtract
        li      $s2, 42     #multiply
        li      $s3, 47     #divide
        li      $s4, -9999

    # Ask user to input integer.
read:
        li      $v0, 4      #input first integer
        la      $a0, pr1
        syscall
        li      $v0, 5
        syscall
        move    $t1, $v0

        li      $v0, 5      #input operation
        la      $a0, pr2
        syscall
        li      $v0, 5
        syscall
        move    $t2, $v0

        li      $v0, 4      #input second integer
        la      $a0, pr1
        syscall
        li      $v0, 5
        syscall
        move    $t3, $v0
        syscall

        beq     $t2, $s0, add    #check for add operation
        beq     $t2, $s1, sub    #check for subtract
        beq     $t2, $s2, mult   #check for multiply
        beq     $t2, $s3, div     #check for divide
        beq     $t1, $s4, exit    #check for signal to exit

        j read

add:

        add     $t1, $t1, $t3
        li      $v0, 4
        la      $a0, smsg
        syscall
        li      $v0, 1
        move    $a0, $t1
        syscall
        li      $v0, 4
        la      $a0, nl
        syscall

sub:

        sub     $t1, $t1, $t3
        li      $v0, 4
        la      $a0, smsg
        syscall
        li      $v0, 1
        move    $a0, $t1
        syscall
        li      $v0, 4
        la      $a0, nl
        syscall


mult:

        mult    $t1, $t3
        li      $v0, 4
        la      $a0, pmsg
        syscall
        li      $v0, 1
        move    $a0, $t1
        syscall
        li      $v0, 4
        la      $a0, nl
        syscall


div:

        beq     $t3, $zero, zero    #check for 0 in divisor
        div     $t1, $t3
        li      $v0, 4
        la      $a0, qmsg
        syscall
        li      $v0, 1
        move    $a0, $t1
        syscall
        li      $v0, 4
        la      $a0, nl
        syscall

zero:


        li      $v0, 4
        la      $a0, zmsg
        syscall
        li      $v0, 4
        la      $a0, nl
        syscall


exit:


        li      $v0, 10
        syscall

Sulaco
(14 items)
 
 
MacBook Pro
(4 items)
 
CPUMotherboardGraphicsRAM
Phenom II X6 1090T Asus Crosshair IV Formula Sapphire 7950 3GB 2x2GB Mushkin Enhanced Blackline  
Hard DriveOSMonitorPower
2x150GB Velociraptor RAID 0 | 2x1TB Hitachi Windows 8 Asus VH242H OCZ ModXStream 700W 
Case
Cooler Master HAF 932 
CPUGraphicsOSMonitor
Core 2 Duo P8400 GeForce 9400M OSX Yosemite 13.3" LED-backlit 
  hide details  
Reply
Sulaco
(14 items)
 
 
MacBook Pro
(4 items)
 
CPUMotherboardGraphicsRAM
Phenom II X6 1090T Asus Crosshair IV Formula Sapphire 7950 3GB 2x2GB Mushkin Enhanced Blackline  
Hard DriveOSMonitorPower
2x150GB Velociraptor RAID 0 | 2x1TB Hitachi Windows 8 Asus VH242H OCZ ModXStream 700W 
Case
Cooler Master HAF 932 
CPUGraphicsOSMonitor
Core 2 Duo P8400 GeForce 9400M OSX Yosemite 13.3" LED-backlit 
  hide details  
Reply
post #2 of 8
I haven't touched Assembly in a decade.... but do some basic debugging. Comment all the branches except one and test. Is it add, subtract, multiple, divide, or exit that is causing the problem?

Also, you can steamline your code..... Instead of a seperate subtract branch, you could just multiple by -1 and use the addition branch again.
Once again...
(13 items)
 
  
CPUMotherboardGraphicsRAM
i7 920 [4.28GHz, HT] Asus P6T + Broadcom NetXtreme II VisionTek HD5850 [900/1200] + Galaxy GT240 2x4GB G.Skill Ripjaw X [1632 MHz] 
Hard DriveOSMonitorKeyboard
Intel X25-M 160GB + 3xRAID0 500GB 7200.12 Window 7 Pro 64 Acer H243H + Samsung 226BW XARMOR-U9BL  
PowerCaseMouseMouse Pad
Antec Truepower New 750W Li Lian PC-V2100 [10x120mm fans] Logitech G9 X-Trac Pro 
  hide details  
Reply
Once again...
(13 items)
 
  
CPUMotherboardGraphicsRAM
i7 920 [4.28GHz, HT] Asus P6T + Broadcom NetXtreme II VisionTek HD5850 [900/1200] + Galaxy GT240 2x4GB G.Skill Ripjaw X [1632 MHz] 
Hard DriveOSMonitorKeyboard
Intel X25-M 160GB + 3xRAID0 500GB 7200.12 Window 7 Pro 64 Acer H243H + Samsung 226BW XARMOR-U9BL  
PowerCaseMouseMouse Pad
Antec Truepower New 750W Li Lian PC-V2100 [10x120mm fans] Logitech G9 X-Trac Pro 
  hide details  
Reply
post #3 of 8
Thread Starter 
Quote:
Originally Posted by DuckieHo;12929799 
I haven't touched Assembly in a decade.... but do some basic debugging. Comment all the branches except one and test. Is it add, subtract, multiple, divide, or exit that is causing the problem?

Also, you can steamline your code..... Instead of a seperate subtract branch, you could just multiple by -1 and use the addition branch again.

Ooh... good idea. I found out one of my problems. My labels were opcodes, and it wouldn't let me use them. Kinda like you can't call variables in C++ names that are functions like if or for...
Sulaco
(14 items)
 
 
MacBook Pro
(4 items)
 
CPUMotherboardGraphicsRAM
Phenom II X6 1090T Asus Crosshair IV Formula Sapphire 7950 3GB 2x2GB Mushkin Enhanced Blackline  
Hard DriveOSMonitorPower
2x150GB Velociraptor RAID 0 | 2x1TB Hitachi Windows 8 Asus VH242H OCZ ModXStream 700W 
Case
Cooler Master HAF 932 
CPUGraphicsOSMonitor
Core 2 Duo P8400 GeForce 9400M OSX Yosemite 13.3" LED-backlit 
  hide details  
Reply
Sulaco
(14 items)
 
 
MacBook Pro
(4 items)
 
CPUMotherboardGraphicsRAM
Phenom II X6 1090T Asus Crosshair IV Formula Sapphire 7950 3GB 2x2GB Mushkin Enhanced Blackline  
Hard DriveOSMonitorPower
2x150GB Velociraptor RAID 0 | 2x1TB Hitachi Windows 8 Asus VH242H OCZ ModXStream 700W 
Case
Cooler Master HAF 932 
CPUGraphicsOSMonitor
Core 2 Duo P8400 GeForce 9400M OSX Yosemite 13.3" LED-backlit 
  hide details  
Reply
post #4 of 8
Thread Starter 
Bump/update
I got my code to do what I want it to, there's just one thing that's bugging me. Whenever I print out my "Please enter an integer" message, it waits for me to press enter before it moves on. Whenever I print out my "Please enter an operation" message, it doesn't wait for enter, it just goes on to the next thing as soon as I push an operation button. Any clue why it's doing that? I'd like for it to wait for enter if possible.
Code:
#Program: calc.s
#Program description: Simple calculator that takes 2 integers #and adds, subtracts, multiplies, or divides them.
#Name: Sam Wright

.data
pr1:.asciiz"Input integer>"
pr2:.asciiz"Input an operation (choices are +, -, *, /)>"
smsg:.asciiz"The sum is: "
pmsg:.asciiz"The product is: "
qmsg:.asciiz"The quotient is: "
rmsg: .asciiz"The remainder is: "
zmsg:.asciiz"Cannot divide by 0"
nl:.asciiz"\n"
word:.word 4

.text
.globl main
main:

li$s0, 43#add
li$s1, 45#subtract
li$s2, 42#multiply
li$s3, 47#divide
li$s4, -9999
li$s5, -1

# Ask user to input integer.
read:
li$v0, 4#input first integer
la$a0, pr1
syscall
li$v0, 5
syscall
move$t1, $v0

beq$t1, $s4, exit

li$v0, 4#input operation
la$a0, pr2
syscall
li$v0, 12
syscall
move$t2, $v0
li$v0, 4
la$a0, nl
syscall

li$v0, 4#input second integer
la$a0, pr1
syscall
li$v0, 5
syscall
move$t3, $v0

beq$s0, $t2, addition
beq$s1, $t2, sign
beq$s2, $t2, multiply
beq$s3, $t2, divide

sign:

mult$t3, $s5
mflo$t3
jaddition

addition:

add$t1, $t1, $t3
li$v0, 4
la$a0, smsg
syscall
li$v0, 1
move$a0, $t1
syscall
li$v0, 4
la$a0, nl
syscall

j read


multiply:

mult$t1, $t3
mflo$t1
li$v0, 4
la$a0, pmsg
syscall
li$v0, 1
move$a0, $t1
syscall
li$v0, 4
la$a0, nl
syscall

j read

divide:

beq$t3, $zero, zero
div$t1, $t3
mflo$t1
mfhi$t3
li$v0, 4
la$a0, qmsg
syscall
li$v0, 1
move$a0, $t1
syscall
li$v0, 4
la$a0, nl
syscall

bgtz$t3, print_rem

j read

print_rem:

li$v0, 4
la$a0, rmsg
syscall
li$v0, 1
move$a0, $t3
syscall
li$v0, 4
la$a0, nl
syscall

j read

zero:


li$v0, 4
la$a0, zmsg
syscall
li$v0, 4
la$a0, nl
syscall

j read

exit:


li$v0, 10
syscall
Sulaco
(14 items)
 
 
MacBook Pro
(4 items)
 
CPUMotherboardGraphicsRAM
Phenom II X6 1090T Asus Crosshair IV Formula Sapphire 7950 3GB 2x2GB Mushkin Enhanced Blackline  
Hard DriveOSMonitorPower
2x150GB Velociraptor RAID 0 | 2x1TB Hitachi Windows 8 Asus VH242H OCZ ModXStream 700W 
Case
Cooler Master HAF 932 
CPUGraphicsOSMonitor
Core 2 Duo P8400 GeForce 9400M OSX Yosemite 13.3" LED-backlit 
  hide details  
Reply
Sulaco
(14 items)
 
 
MacBook Pro
(4 items)
 
CPUMotherboardGraphicsRAM
Phenom II X6 1090T Asus Crosshair IV Formula Sapphire 7950 3GB 2x2GB Mushkin Enhanced Blackline  
Hard DriveOSMonitorPower
2x150GB Velociraptor RAID 0 | 2x1TB Hitachi Windows 8 Asus VH242H OCZ ModXStream 700W 
Case
Cooler Master HAF 932 
CPUGraphicsOSMonitor
Core 2 Duo P8400 GeForce 9400M OSX Yosemite 13.3" LED-backlit 
  hide details  
Reply
post #5 of 8
It waits when you print out "Input integer>" both times?

However, does not wait when you print "Input an operation (choices are +, -, *, /)>"?


How about the other messages? Does the system wait?
Once again...
(13 items)
 
  
CPUMotherboardGraphicsRAM
i7 920 [4.28GHz, HT] Asus P6T + Broadcom NetXtreme II VisionTek HD5850 [900/1200] + Galaxy GT240 2x4GB G.Skill Ripjaw X [1632 MHz] 
Hard DriveOSMonitorKeyboard
Intel X25-M 160GB + 3xRAID0 500GB 7200.12 Window 7 Pro 64 Acer H243H + Samsung 226BW XARMOR-U9BL  
PowerCaseMouseMouse Pad
Antec Truepower New 750W Li Lian PC-V2100 [10x120mm fans] Logitech G9 X-Trac Pro 
  hide details  
Reply
Once again...
(13 items)
 
  
CPUMotherboardGraphicsRAM
i7 920 [4.28GHz, HT] Asus P6T + Broadcom NetXtreme II VisionTek HD5850 [900/1200] + Galaxy GT240 2x4GB G.Skill Ripjaw X [1632 MHz] 
Hard DriveOSMonitorKeyboard
Intel X25-M 160GB + 3xRAID0 500GB 7200.12 Window 7 Pro 64 Acer H243H + Samsung 226BW XARMOR-U9BL  
PowerCaseMouseMouse Pad
Antec Truepower New 750W Li Lian PC-V2100 [10x120mm fans] Logitech G9 X-Trac Pro 
  hide details  
Reply
post #6 of 8
Thread Starter 
Quote:
Originally Posted by DuckieHo;12934934 
It waits when you print out "Input integer>" both times?

However, does not wait when you print "Input an operation (choices are +, -, *, /)>"?


How about the other messages? Does the system wait?

Yes, it waits for me to push enter for the "Input integer>" For example, I run my code, it says "Input integer>". I push 5. It doesn't move on to the next thing until I push enter. I push enter. It says "Input an operation... etc". I push the "+" button. It immediately brings up the "Input integer>" message again. I put in 5. I hit enter. It prints out "The sum is: 10", and then goes to the next line and starts all over again. It will run until I put -9999 in as my first integer. Basically the only time it waits for enter is when I'm reading in integers. I wouldn't think it would have anything to do with me reading in a character instead...
Sulaco
(14 items)
 
 
MacBook Pro
(4 items)
 
CPUMotherboardGraphicsRAM
Phenom II X6 1090T Asus Crosshair IV Formula Sapphire 7950 3GB 2x2GB Mushkin Enhanced Blackline  
Hard DriveOSMonitorPower
2x150GB Velociraptor RAID 0 | 2x1TB Hitachi Windows 8 Asus VH242H OCZ ModXStream 700W 
Case
Cooler Master HAF 932 
CPUGraphicsOSMonitor
Core 2 Duo P8400 GeForce 9400M OSX Yosemite 13.3" LED-backlit 
  hide details  
Reply
Sulaco
(14 items)
 
 
MacBook Pro
(4 items)
 
CPUMotherboardGraphicsRAM
Phenom II X6 1090T Asus Crosshair IV Formula Sapphire 7950 3GB 2x2GB Mushkin Enhanced Blackline  
Hard DriveOSMonitorPower
2x150GB Velociraptor RAID 0 | 2x1TB Hitachi Windows 8 Asus VH242H OCZ ModXStream 700W 
Case
Cooler Master HAF 932 
CPUGraphicsOSMonitor
Core 2 Duo P8400 GeForce 9400M OSX Yosemite 13.3" LED-backlit 
  hide details  
Reply
post #7 of 8
Thread Starter 
Bump
Sulaco
(14 items)
 
 
MacBook Pro
(4 items)
 
CPUMotherboardGraphicsRAM
Phenom II X6 1090T Asus Crosshair IV Formula Sapphire 7950 3GB 2x2GB Mushkin Enhanced Blackline  
Hard DriveOSMonitorPower
2x150GB Velociraptor RAID 0 | 2x1TB Hitachi Windows 8 Asus VH242H OCZ ModXStream 700W 
Case
Cooler Master HAF 932 
CPUGraphicsOSMonitor
Core 2 Duo P8400 GeForce 9400M OSX Yosemite 13.3" LED-backlit 
  hide details  
Reply
Sulaco
(14 items)
 
 
MacBook Pro
(4 items)
 
CPUMotherboardGraphicsRAM
Phenom II X6 1090T Asus Crosshair IV Formula Sapphire 7950 3GB 2x2GB Mushkin Enhanced Blackline  
Hard DriveOSMonitorPower
2x150GB Velociraptor RAID 0 | 2x1TB Hitachi Windows 8 Asus VH242H OCZ ModXStream 700W 
Case
Cooler Master HAF 932 
CPUGraphicsOSMonitor
Core 2 Duo P8400 GeForce 9400M OSX Yosemite 13.3" LED-backlit 
  hide details  
Reply
post #8 of 8
Thread Starter 
bump
Sulaco
(14 items)
 
 
MacBook Pro
(4 items)
 
CPUMotherboardGraphicsRAM
Phenom II X6 1090T Asus Crosshair IV Formula Sapphire 7950 3GB 2x2GB Mushkin Enhanced Blackline  
Hard DriveOSMonitorPower
2x150GB Velociraptor RAID 0 | 2x1TB Hitachi Windows 8 Asus VH242H OCZ ModXStream 700W 
Case
Cooler Master HAF 932 
CPUGraphicsOSMonitor
Core 2 Duo P8400 GeForce 9400M OSX Yosemite 13.3" LED-backlit 
  hide details  
Reply
Sulaco
(14 items)
 
 
MacBook Pro
(4 items)
 
CPUMotherboardGraphicsRAM
Phenom II X6 1090T Asus Crosshair IV Formula Sapphire 7950 3GB 2x2GB Mushkin Enhanced Blackline  
Hard DriveOSMonitorPower
2x150GB Velociraptor RAID 0 | 2x1TB Hitachi Windows 8 Asus VH242H OCZ ModXStream 700W 
Case
Cooler Master HAF 932 
CPUGraphicsOSMonitor
Core 2 Duo P8400 GeForce 9400M OSX Yosemite 13.3" LED-backlit 
  hide details  
Reply
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Coding and Programming
Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Oh yeah, it's assembly language (MIPS)