7

So, I have no idea how assembly works or what I'm doing. I thought I did, but of course I was wrong. So here's my question - I don't even know how to let a user enter an integer so I can store it in memory. I also don't know if my variables are aligned because I don't even understand what "alignment" really is. Below is my assembly code, along with comments demonstrating what I'd LIKE for the code to be doing. Please help

.data
                    # variables here

intPrompt:                  .asciiz "\nPlease enter an integer.\n"
stringPrompt:               .asciiz "\nPlease enter a string that is less than 36 (35 or less) characters long.\n"
charPrompt:                 .asciiz "\nPlease enter a single character.\n"
int:                    .space 4
string:                     .space 36
char:                   .byte  1







                    .text
                    .globl main
main:

                    # print the first prompt
                    li $v0, 4
                    la $a0, intPrompt
                    syscall


                    # allow user to enter an integer
                    li $v0, 5
                    syscall

                    # store the input in `int`
                    # don't really know what to do right here, I want to save the user inputed integer into 'int' variable
                    sw $v0, int
                    syscall
Eric Diviney
  • 327
  • 2
  • 5
  • 16
  • Your `int` is probably not aligned because it comes after variable length strings that are unlikely to have total length that is a multiple of 4. You should place your `int` at the beginning of the section, then you can be sure it's aligned, or use alignment directives. `sw $v0, int` is fine, although it is a pseudo-op. – Jester Mar 04 '15 at 00:58
  • 1
    `syscall` returns the integer in `$v0`. All you need to do is store that into `int` with `sw $v0, int`. Oh - that's what you've done. (No need for another call to `syscall`, though). I wouldn't use names like `int`, `string`, etc. They're probably OK here, but you're likely to run up against reserved words in other languages. –  Mar 04 '15 at 00:59
  • This is the error I get - Error in [directory]/Assignment2PartA.asm line 32 column 6: "sw": Too few or incorrectly formatted operands. Expected: sw $t1,-100($t2) @HoboSapiens – Eric Diviney Mar 04 '15 at 01:02
  • Then your assembler does not understand this pseudo-op. Use `la $t0, int; sw $v0, ($t0)` or similar. Of couse `la` is another pseudo-op but surely it understands **that** :) – Jester Mar 04 '15 at 01:03
  • @Jester - sorry to bother you - i have one more question. When I run `li $v0, 1; la $a0, int; syscall;` the output I get is "268500992" - any idea why? – Eric Diviney Mar 04 '15 at 01:34
  • Yes, the write integer system call expects the integer itself not its address. You need to load it from memory, that is do `li $v0, 1; la $a0, int; lw $a0, ($a0); syscall;` – Jester Mar 04 '15 at 01:38

1 Answers1

2

You should change type of "int" variable from .space to .word

finally it should look like this:

.data
                    # variables here

    intPrompt:                .asciiz "\nPlease enter an integer.\n"
    stringPrompt:             .asciiz "\nPlease enter a string that is less than 36 (35 or less) characters long.\n"
    charPrompt:               .asciiz "\nPlease enter a single character.\n"
    int:                      .word
    string:                   .space 36
    char:                     .byte  1

main:

    li $v0, 4         #you say to program, that you're going to output string which will be in the $a0 register
    la $a0, intPrompt #here you load your string from intPromt var. to $a0
    syscall           #this command just executes everything that you have written before >> it prints string, which is in $a0

    li $v0, 5         #this command says: "Hey, read an integer from console and put it in $v0!"
    syscall           #this command executes all previous commands ( li $v0, 5 )

    sw $v0, int       #sw -> store word, here you move value from $v0 to "int" variable
    syscall           #executes (sw $v0, int), here you have your input number in "int" variable

You can find more detailed information here