int
decode_lsr(int opcode) {
if ((opcode & 0xf) != 0x6)
return -1;
if ((opcode >> 9 & 0x7f) != 0x4a)
return -1;
return (opcode >> 4) & 0x1f;
}
; print the first 6 powers of two
; j = 1;
; for (i = 0; i != 6; i++) {
; printf("%d\n", j);
; j = j + j;
; }
; i in r20
; j in r22
; r27 is used to hold a temporary value
;
LDI r20, 0 ; i = 0
LDI r22, 1 ; j = 1
L1:
LDI r24, 1 ; printf("%d\n", i);
MOV r26, r22
LDI r31, 0x70
LDI r30, 0x00
ICALL
ADD r22, r22
LDI r27, 1
ADD r20, r27 ; i++
LDI r27, 6
SUB r27, r20
BRBC 1, L1
LDI r24, 0 ; exit
LDI r31, 0x70
LDI r30, 0x00
ICALL
; 600 = 0x258, 700 = 0x2bc, 800=0x320 ; R1 <- SRAM[600] LDI R29,0x02 LDI R28,0x58 LDD R1,Y+0 ; R2 <- SRAM[700] LDI R29,0x02 LDI R28,0xBC LDD R2,Y+0 ; SRAM[800] <- R1 + R2 ADD R1,R2 LDI R29,0x03 LDI R28,0x20 STD Y+0,R1
#include &stdio.h>
#include <ctype.h>
enum {
MAX_LINE_LENGTH = 256,
MAX_WORD_LENGTH = 10,
MAX_WORDS = 100
};
int
main(int argc, char *args[]) {
char words[MAX_WORDS][MAX_WORD_LENGTH+1];
int n_words, word_position, i;
printf("Enter comma separated words: ");
n_words = 0;
word_position = 0;
while (1) {
int c = getchar();
if (c == EOF || c == ',' || c == '\n' || c == '\0') {
words[n_words][word_position] = '\0';
n_words++;
word_position = 0;
if (c != ',')
break;
if (n_words == MAX_WORDS) {
printf("limit of %d words exceeded\n", MAX_WORDS);
return 1;
}
continue;
}
words[n_words][word_position] = c;
word_position++;
if (word_position == MAX_WORD_LENGTH) {
printf("limit on word length(%d) exceeded\n", MAX_WORD_LENGTH);
return 1;
}
}
for (i = 0; i < n_words; i++)
printf("%s\n", &words[i][0]);
return 0;
}
FOR each customer DO IF any of this customer's accounts are larger than the biggest seen so far THEN this customer is richest seen so far END END
Code to find the name of the customer with the largest balance in a single account.
enum {
MAX_ACCOUNTS = 5,
N_CUSTOMERS = 1000
};
struct customer {
char name[20];
int numberOfAccounts;
double balance[MAX_ACCOUNTS];
};
struct customer customers[N_CUSTOMERS];
...
int i, richest;
double maxbalance;
...
richest = 0; /* first is richest seen so far ... */
for (i = 1; i < N_CUSTOMERS; ++i) {
for (j = 0; j < customers[i].numberOfAccounts; ++ j) {
if (customers[i].balance[j] > maxBalance) {
richest = i;
maxBalance = customers[i].balance[j];
}
}
}
printf("Richest customer is %s\n", customers[richest].name);